xref: /petsc/src/sys/tests/ex68.c (revision 2ff79c18c26c94ed8cb599682f680f231dca6444)
1 const char help[] = "Test PetscLogEventsPause() and PetscLogEventsUnpause()";
2 
3 #include <petscsys.h>
4 
5 int main(int argc, char **argv)
6 {
7   const PetscInt  num_log_events = 4;
8   PetscLogStage   main_stage, unrelated_stage_1, unrelated_stage_2;
9   PetscLogEvent   runtime_event, unrelated_event[4];
10   PetscLogHandler default_handler;
11   PetscClassId    runtime_classid, unrelated_classid[4];
12   PetscBool       main_visible      = PETSC_FALSE;
13   PetscBool       unrelated_visible = PETSC_FALSE;
14   PetscBool       get_main_visible;
15   PetscBool       get_unrelated_1_visible;
16   PetscBool       get_unrelated_2_visible;
17   PetscBool       is_active;
18 
19   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
20   PetscCall(PetscLogIsActive(&is_active));
21   PetscCheck(is_active, PETSC_COMM_WORLD, PETSC_ERR_SUP, "Logging must be active for this test");
22   PetscCall(PetscLogActions(PETSC_FALSE));
23   PetscCall(PetscLogObjects(PETSC_FALSE));
24 
25   PetscOptionsBegin(PETSC_COMM_WORLD, NULL, help, NULL);
26   PetscCall(PetscOptionsBool("-main_visible", "The logging visibility of the main stage", NULL, main_visible, &main_visible, NULL));
27   PetscCall(PetscOptionsBool("-unrelated_visible", "The logging visibility of the unrelated stage", NULL, unrelated_visible, &unrelated_visible, NULL));
28   PetscOptionsEnd();
29 
30   /* This test simulates a program with unrelated logging stages and events
31      that has to "stop the world" to lazily initialize a runtime.
32 
33      - Pausing events should send the log data for the runtime initialization
34        to the Main Stage
35 
36      - Turning the Main Stage invisible should hide it from -log_view
37 
38      So the runtime initialization should be more or less missing from -log_view. */
39 
40   PetscCall(PetscClassIdRegister("External runtime", &runtime_classid));
41   PetscCall(PetscLogEventRegister("External runtime initialization", runtime_classid, &runtime_event));
42 
43   for (PetscInt i = 0; i < num_log_events; i++) {
44     char name[32];
45 
46     PetscCall(PetscSNPrintf(name, sizeof(name) / sizeof(char), "Unrelated event %" PetscInt_FMT, i));
47     PetscCall(PetscClassIdRegister(name, &unrelated_classid[i]));
48     PetscCall(PetscLogEventRegister(name, unrelated_classid[i], &unrelated_event[i]));
49   }
50   PetscCall(PetscLogStageRegister("Unrelated stage 1", &unrelated_stage_1));
51   PetscCall(PetscLogStageRegister("Unrelated stage 2", &unrelated_stage_2));
52   PetscCall(PetscLogStageGetId("Main Stage", &main_stage));
53   PetscCall(PetscLogStageSetVisible(main_stage, main_visible));
54   PetscCall(PetscLogStageSetVisible(unrelated_stage_1, unrelated_visible));
55   PetscCall(PetscLogStageSetVisible(unrelated_stage_2, unrelated_visible));
56   PetscCall(PetscLogGetDefaultHandler(&default_handler));
57   if (default_handler) {
58     PetscCall(PetscLogStageGetVisible(main_stage, &get_main_visible));
59     PetscCall(PetscLogStageGetVisible(unrelated_stage_1, &get_unrelated_1_visible));
60     PetscCall(PetscLogStageGetVisible(unrelated_stage_2, &get_unrelated_2_visible));
61     PetscCheck(main_visible == get_main_visible, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Get/Set stage visibility discrepancy");
62     PetscCheck(unrelated_visible == get_unrelated_1_visible, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Get/Set stage visibility discrepancy");
63     PetscCheck(unrelated_visible == get_unrelated_2_visible, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Get/Set stage visibility discrepancy");
64   }
65 
66   PetscCall(PetscLogStagePush(unrelated_stage_1));
67   PetscCall(PetscLogEventBegin(unrelated_event[0], NULL, NULL, NULL, NULL));
68   PetscCall(PetscSleep(0.05));
69   PetscCall(PetscLogEventBegin(unrelated_event[1], NULL, NULL, NULL, NULL));
70   PetscCall(PetscSleep(0.05));
71   PetscCall(PetscLogStagePush(unrelated_stage_2));
72   PetscCall(PetscLogEventBegin(unrelated_event[2], NULL, NULL, NULL, NULL));
73   PetscCall(PetscSleep(0.05));
74   PetscCall(PetscLogEventBegin(unrelated_event[3], NULL, NULL, NULL, NULL));
75   PetscCall(PetscSleep(0.05));
76   PetscCall(PetscLogEventsPause());
77   PetscCall(PetscLogEventBegin(runtime_event, NULL, NULL, NULL, NULL));
78   PetscCall(PetscSleep(0.2));
79   PetscCall(PetscLogEventEnd(runtime_event, NULL, NULL, NULL, NULL));
80   PetscCall(PetscLogEventsResume());
81   PetscCall(PetscLogEventEnd(unrelated_event[3], NULL, NULL, NULL, NULL));
82   PetscCall(PetscSleep(0.05));
83   PetscCall(PetscLogEventEnd(unrelated_event[2], NULL, NULL, NULL, NULL));
84   PetscCall(PetscSleep(0.05));
85   PetscCall(PetscLogStagePop());
86   PetscCall(PetscSleep(0.05));
87   PetscCall(PetscLogEventEnd(unrelated_event[1], NULL, NULL, NULL, NULL));
88   PetscCall(PetscSleep(0.05));
89   PetscCall(PetscLogEventEnd(unrelated_event[0], NULL, NULL, NULL, NULL));
90   PetscCall(PetscLogStagePop());
91   { // test of PetscLogStageGetPerfInfo()
92     PetscLogHandler handler;
93 
94     PetscCall(PetscLogGetDefaultHandler(&handler));
95     if (handler) {
96       PetscEventPerfInfo stage_info;
97 
98       PetscCall(PetscLogStageGetPerfInfo(unrelated_stage_1, &stage_info));
99       (void)stage_info;
100     }
101   }
102   PetscCall(PetscFinalize());
103   return 0;
104 }
105 
106 /*TEST
107 
108   # main stage invisible, "External runtime initialization" shouldn't appear in the log
109   test:
110     requires: defined(PETSC_USE_LOG)
111     suffix: 0
112     args: -log_view -unrelated_visible -log_view_memory
113     filter: grep -o "\\(External runtime initialization\\|Unrelated event\\)"
114 
115   # unrelated stage invisible, "Unrelated event" shouldn't appear in the log
116   test:
117     requires: defined(PETSC_USE_LOG)
118     suffix: 1
119     args: -log_view -main_visible -log_view_memory
120     filter: grep -o "\\(External runtime initialization\\|Unrelated event\\)"
121 
122 TEST*/
123