1 : /*
2 : * Copyright (c) 2011 The Native Client Authors. All rights reserved.
3 : * Use of this source code is governed by a BSD-style license that can be
4 : * found in the LICENSE file.
5 : */
6 :
7 : #include <stdio.h>
8 :
9 : #include "native_client/src/include/nacl_assert.h"
10 : #include "native_client/src/include/portability.h"
11 : #include "native_client/src/include/portability_io.h"
12 : #include "native_client/src/include/portability_string.h"
13 : #include "native_client/src/shared/platform/nacl_log.h"
14 : #include "native_client/src/shared/platform/nacl_time.h"
15 :
16 : #include "native_client/src/trusted/perf_counter/nacl_perf_counter.h"
17 :
18 : /* A simple test of the performance counter basics. */
19 :
20 : struct perf_counter_test {
21 : struct nacl_abi_timeval _[2];
22 : int64_t res;
23 : };
24 :
25 : struct perf_counter_test arr[] = {
26 : { { { 0, 0 }, { 0, 0 } }, 0 },
27 : { { { 0, 100 }, { 0, 0 } }, -100 },
28 : { { { 1, 100, }, { 2, 5 } }, (999*1000 + 905) },
29 : { { { 0, 0 }, { 0, 0 } }, 0 },
30 : };
31 :
32 :
33 : #define ALEN(A) ((int)(sizeof(A)/sizeof(A[0])))
34 :
35 1 : int main(int argc, char*argv[]) {
36 1 : int i = 0;
37 1 : int64_t res = 0;
38 : UNREFERENCED_PARAMETER(argc);
39 : UNREFERENCED_PARAMETER(argv);
40 1 : NaClLogModuleInit();
41 1 : NaClTimeInit();
42 :
43 1 : for (; i < ALEN(arr)-1; ++i) {
44 : struct NaClPerfCounter tm;
45 1 : memset(&tm, 0, sizeof(struct NaClPerfCounter));
46 :
47 1 : strncpy(tm.app_name, "mocked app", ALEN(tm.app_name));
48 :
49 1 : tm.sample_list[0] = arr[i]._[0];
50 1 : tm.sample_list[1] = arr[i]._[1];
51 1 : tm.samples = 2;
52 1 : res = NaClPerfCounterInterval(&tm, 0, 1);
53 1 : ASSERT_EQ(res, arr[i].res);
54 1 : }
55 :
56 : do {
57 : /* live run */
58 : unsigned i;
59 : struct NaClPerfCounter tm;
60 1 : NaClPerfCounterCtor(&tm, "test_perf_counter");
61 :
62 1 : ASSERT_EQ(1, tm.samples);
63 :
64 1 : for (i = 1; i < NACL_MAX_PERF_COUNTER_SAMPLES-2; ++i) {
65 1 : NaClPerfCounterMark(&tm, "foo");
66 1 : ASSERT_EQ(i+1, tm.samples);
67 1 : }
68 1 : } while (0);
69 :
70 : do {
71 : /* live run */
72 : unsigned i;
73 : struct NaClPerfCounter tm;
74 1 : int r = 0;
75 : const char *names[] = {
76 1 : "--",
77 1 : "first",
78 1 : "second",
79 1 : "third",
80 1 : "fourth",
81 1 : "fifth",
82 1 : "sixth",
83 : /* Note: this name must be longer than NACL_MAX_PERF_COUNTER_NAME */
84 1 : "a-really-long-name-which-will-be-truncated",
85 1 : "eighth",
86 1 : "ninth",
87 1 : "tenth",
88 1 : "eleventh",
89 1 : "twelveth",
90 1 : "thirteenth",
91 1 : "14th",
92 1 : "15th",
93 : };
94 1 : ASSERT_EQ(NACL_MAX_PERF_COUNTER_SAMPLES, ALEN(names));
95 :
96 1 : NaClPerfCounterCtor(&tm, "test_perf_counter");
97 :
98 1 : ASSERT_EQ(1, tm.samples);
99 :
100 1 : for (i = 1; i < ALEN(names); ++i) {
101 : NaClLog(1, "NaCl_Perf_Counter_Test: testing event name %d: %s\n",
102 1 : i, names[i]);
103 1 : r = NaClPerfCounterMark(&tm, names[i]);
104 1 : ASSERT_EQ(i+1, tm.samples);
105 1 : ASSERT_EQ(i, (unsigned)r);
106 : ASSERT_EQ(0, strncmp(tm.sample_names[i], names[i], \
107 1 : (NACL_MAX_PERF_COUNTER_NAME-1)));
108 1 : }
109 : NaClLog(1, "NaCl_Perf_Counter_Test: checking truncated name: %s vs %s\n",
110 1 : tm.sample_names[7], names[7]);
111 1 : ASSERT_EQ(NACL_MAX_PERF_COUNTER_SAMPLES, tm.samples);
112 1 : ASSERT_EQ(strlen(tm.sample_names[7]), (NACL_MAX_PERF_COUNTER_NAME-1));
113 :
114 1 : NaClLog(1, "NaCl_Perf_Counter_Test: checking event overflow\n");
115 1 : r = NaClPerfCounterMark(&tm, "foo");
116 1 : ASSERT_EQ(NACL_MAX_PERF_COUNTER_SAMPLES, tm.samples);
117 1 : ASSERT_EQ(-1, r);
118 1 : ASSERT_NE(-1, NaClPerfCounterInterval(&tm, 0, tm.samples - 1));
119 1 : } while (0);
120 :
121 1 : NaClTimeFini();
122 1 : NaClLogModuleFini();
123 1 : return 0;
124 1 : }
|