aboutsummaryrefslogtreecommitdiff
path: root/test/check_ppamltracer.c
blob: e842789ce057a6c44a0e6b7a8dc2b76ad0bbc8e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/* check_ppamltracer.c -- test program for ppamltracer
 * Copyright (C) 2014  Galois, Inc.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * To contact Galois, complete the Web form at
 * <http://corp.galois.com/contact/> or write to Galois, Inc., 421 Southwest
 * 6th Avenue, Suite 300, Portland, Oregon, 97204-1622. */

#ifdef HAVE_CONFIG_H
#	include <config.h>
#endif

#define _POSIX_C_SOURCE 200112L

#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#if HAVE_TIME_H
#	include <time.h>
#endif
#if HAVE_UNISTD_H
#	include <unistd.h>
#endif

#include <check.h>

#include <otf.h>
#include <ppaml/tracer.h>

#include "tmpdir.h"
#include "trace_invalid.h"
#include "trace_valid.h"

#define ignore(x) ((void)(x))

#if (HAVE_FIXTURE_tmpdir && HAVE_FIXTURE_trace_invalid && HAVE_UNISTD_H)
#	define HAVE_TEST_init_creates_trace
	START_TEST(test_init_creates_trace)
	{
		char index_file[128];
		strcpy(index_file, trace_invalid_path());
		strcat(index_file, ".otf");
		ck_assert(access(index_file, F_OK) == 0);
	}
	END_TEST
#endif

#if (HAVE_FIXTURE_tmpdir && HAVE_UNISTD_H && HAVE_SETENV)
#	define HAVE_TEST_init_from_env
	START_TEST(test_init_from_env)
	{
		// Figure out where to put the trace.
		char trace_base[128];
		strcpy(trace_base, tmpdir_name());
		strcat(trace_base, "/trace");
		// Tell ppamltracer where.
		ck_assert(
			setenv("PPAMLTRACER_TRACE_BASE", trace_base, 1) == 0);
		// Create the trace.
		ppaml_tracer_t tracer;
		ck_assert(
			ppaml_tracer_init_from_env(&tracer) == 0);
		ck_assert(ppaml_tracer_done(&tracer) == 0);
		// Make sure it's in the right place.
		strcat(trace_base, ".otf");
		ck_assert(access(trace_base, F_OK) == 0);
	}
	END_TEST

#	define HAVE_TEST_init_from_env_fails_on_empty
	START_TEST(test_init_from_env_fails_on_empty)
	{
		ck_assert(setenv("PPAMLTRACER_TRACE_BASE", "", 1) == 0);
		ppaml_tracer_t tracer;
		ck_assert(ppaml_tracer_init_from_env(&tracer) == 5);
	}
	END_TEST
#endif

#if (HAVE_FIXTURE_tmpdir && HAVE_UNISTD_H && HAVE_UNSETENV)
#	define HAVE_TEST_init_from_env_fails_on_unset
	START_TEST(test_init_from_env_fails_on_unset)
	{
		ck_assert(unsetenv("PPAMLTRACER_TRACE_BASE") == 0);
		ppaml_tracer_t tracer;
		ck_assert(ppaml_tracer_init_from_env(&tracer) == 5);
	}
	END_TEST
#endif

#if (HAVE_FIXTURE_tmpdir && HAVE_FIXTURE_trace_valid)
#	define HAVE_TEST_simple_creates_valid_trace
	START_TEST(test_simple_creates_valid_trace)
	{
		OTF_FileManager *const manager = OTF_FileManager_open(16);
		ck_assert_msg(manager != NULL, otf_strerr);
		OTF_Reader *const reader =
			OTF_Reader_open(trace_valid_path(), manager);
		ck_assert_msg(reader != NULL, otf_strerr);
		ck_assert_msg(OTF_Reader_close(reader) == 1, otf_strerr);
		OTF_FileManager_close(manager);
	}
	END_TEST

#	define HAVE_TEST_simple_defines_main
	START_TEST(test_simple_defines_main)
	{
		OTF_FileManager *const manager = OTF_FileManager_open(16);
		ck_assert_msg(manager != NULL, otf_strerr);
		OTF_Reader *const reader =
			OTF_Reader_open(trace_valid_path(), manager);
		ck_assert_msg(reader != NULL, otf_strerr);
		ck_assert_msg(OTF_Reader_close(reader) == 1, otf_strerr);
		OTF_FileManager_close(manager);
	}
	END_TEST
#endif

#if (HAVE_FIXTURE_tmpdir && HAVE_TIME_H)
#	define HAVE_TEST_trace_accurate_timing
	static uint64_t test_trace_accurate_timing_delta;

	static int test_trace_accurate_timing_handle_enter(
		void *const userData,
		const uint64_t time,
		const uint32_t function,
		const uint32_t process,
		const uint32_t source)
	{
		ignore(userData);
		ignore(function);
		ignore(process);
		ignore(source);
		test_trace_accurate_timing_delta = time;
		return OTF_RETURN_OK;
	}

	static int test_trace_accurate_timing_handle_exit(
		void *const userData,
		const uint64_t time,
		const uint32_t function,
		const uint32_t process,
		const uint32_t source)
	{
		ignore(userData);
		ignore(function);
		ignore(process);
		ignore(source);
		test_trace_accurate_timing_delta =
			time - test_trace_accurate_timing_delta;
		return OTF_RETURN_OK;
	}

	START_TEST(test_trace_accurate_timing)
	{
		// Create the trace.
		char trace_base[128];
		strcpy(trace_base, tmpdir_name());
		strcat(trace_base, "/trace");
		ppaml_tracer_t tracer;
		ck_assert(
			ppaml_tracer_init(&tracer, trace_base) == 0);
		ppaml_phase_t phase;
		ck_assert(
			ppaml_phase_init(&tracer, &phase, "test phase") == 0);
		// Time a single phase.
		ck_assert(ppaml_phase_start(&phase) == 0);
		const struct timespec hundred_ms =
			{ .tv_sec = 0, .tv_nsec = 100000000 };
		nanosleep(&hundred_ms, NULL);
		ck_assert(ppaml_phase_stop(&phase) == 0);
		// Clean up and close the trace.
		ck_assert(ppaml_phase_done(&phase) == 0);
		ck_assert(ppaml_tracer_done(&tracer) == 0);
		/* Verify that the delta is about 100 ms (100,000 ticks of the
		 * tracer timer). */
		OTF_FileManager *const manager = OTF_FileManager_open(16);
		ck_assert_msg(manager != NULL, otf_strerr);
		OTF_Reader *const reader =
			OTF_Reader_open(trace_base, manager);
		ck_assert_msg(reader != NULL, otf_strerr);
		OTF_HandlerArray *const handlers = OTF_HandlerArray_open();
		ck_assert_msg(handlers != NULL, otf_strerr);
		ck_assert_msg(
			OTF_HandlerArray_setHandler(
				handlers,
				(OTF_FunctionPointer *)test_trace_accurate_timing_handle_enter,
				OTF_ENTER_RECORD)
			== 1,
			otf_strerr);
		ck_assert_msg(
			OTF_HandlerArray_setHandler(
				handlers,
				(OTF_FunctionPointer *)test_trace_accurate_timing_handle_exit,
				OTF_LEAVE_RECORD)
			== 1,
			otf_strerr);
		ck_assert_msg(
			OTF_Reader_readEvents(reader, handlers) != OTF_READ_ERROR,
			otf_strerr);
		/* Require less than +/- 10% to pass.  Yes, this is an
		 * arbitrary metric, but it should trigger if there's a serious
		 * regression. */
		ck_assert_msg(
			90000 <= test_trace_accurate_timing_delta,
			"timed interval (%d us) is more than 10%% fast",
			test_trace_accurate_timing_delta);
		ck_assert_msg(test_trace_accurate_timing_delta < 110000,
			"timed interval (%d us) is more than 10%% slow",
			test_trace_accurate_timing_delta);
		// All done.
		ck_assert_msg(
			OTF_HandlerArray_close(handlers) == 1, otf_strerr);
		ck_assert_msg(OTF_Reader_close(reader) == 1, otf_strerr);
		OTF_FileManager_close(manager);
	}
	END_TEST
#endif

Suite *ppamltracer_suite()
{
	Suite *const s = suite_create("ppamltracer");
	// Test case: init
	TCase *tc = tcase_create("init");
	tmpdir_add_checked(tc);
	trace_invalid_add_checked(tc);
#	ifdef HAVE_TEST_init_creates_trace
		tcase_add_test(tc, test_init_creates_trace);
#	endif
	suite_add_tcase(s, tc);
	// Test case: init from env
	tc = tcase_create("init_from_env");
	tmpdir_add_checked(tc);
#	ifdef HAVE_TEST_init_from_env
		tcase_add_test(tc, test_init_from_env);
#	endif
#	ifdef HAVE_TEST_init_from_env_fails_on_unset
		tcase_add_test(tc, test_init_from_env_fails_on_unset);
#	endif
#	ifdef HAVE_TEST_init_from_env_fails_on_empty
		tcase_add_test(tc, test_init_from_env_fails_on_empty);
#	endif
	suite_add_tcase(s, tc);
	// Test case: valid
	tc = tcase_create("valid");
	tmpdir_add_checked(tc);
	trace_valid_add_checked(tc);
#	ifdef HAVE_TEST_simple_creates_valid_trace
		tcase_add_test(tc, test_simple_creates_valid_trace);
#	endif
#	ifdef HAVE_TEST_simple_defines_main
		tcase_add_test(tc, test_simple_defines_main);
#	endif
	suite_add_tcase(s, tc);
	// Test case: timing-accurate
	tc = tcase_create("accurate");
	tmpdir_add_checked(tc);
#	ifdef HAVE_TEST_trace_accurate_timing
		tcase_add_test(tc, test_trace_accurate_timing);
#	endif
	suite_add_tcase(s, tc);
	return s;
}

int main()
{
	Suite *const s = ppamltracer_suite();
	SRunner *const runner = srunner_create(s);
	srunner_run_all(runner, CK_NORMAL);
	const int n_failed = srunner_ntests_failed(runner);
	srunner_free(runner);
	return (n_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}