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