xref: /petsc/src/sys/tests/ex68.c (revision dbff2e812d3f1975f2fc386c980b1b5f65f538ff)
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 initalization
32        to the Main Stage
33 
34      - Turning the Main Stage invisible should hide it from -log_view
35 
36      So the runtime intialization 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 
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 
64   PetscCall(PetscLogEventEnd(unrelated_event, NULL, NULL, NULL, NULL));
65   PetscCall(PetscLogStagePop());
66   PetscCall(PetscFinalize());
67   return 0;
68 }
69 
70 /*TEST
71 
72   # main stage invisible, "External runtime initialization" shouldn't appear in the log
73   test:
74     requires: defined(PETSC_USE_LOG)
75     suffix: 0
76     args: -log_view -unrelated_visible
77     filter: grep -o "\\(External runtime initialization\\|Unrelated event\\)"
78 
79   # unrelated stage invisible, "Unrelated event" shouldn't appear in the log
80   test:
81     requires: defined(PETSC_USE_LOG)
82     suffix: 1
83     args: -log_view -main_visible
84     filter: grep -o "\\(External runtime initialization\\|Unrelated event\\)"
85 
86 TEST*/
87