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