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