1 | #include <time.h> |
---|
2 | #include <stdio.h> |
---|
3 | #include <stdlib.h> |
---|
4 | #include <string.h> |
---|
5 | |
---|
6 | #include "observation.h" |
---|
7 | |
---|
8 | typedef struct B_UserItem |
---|
9 | BFX_EXTRUSION_DECL(B_UserItem) |
---|
10 | { |
---|
11 | BFX_INTRUSION(B_UserItem) |
---|
12 | int userdata[64]; |
---|
13 | } |
---|
14 | B_UserItem; |
---|
15 | |
---|
16 | BFX_EXTRUSION_FOLLOWUP(B_UserItem) |
---|
17 | |
---|
18 | #if defined(NDEBUG) || (defined(__cforall) && !defined(__CFA_DEBUG__)) |
---|
19 | enum { DefaultNumNodes = 1000, DefaultExperimentDurSec = 1, DefaultCheckDonePeriod = 1000, DefaultExperimentDurOpCount = -1 }; |
---|
20 | #define TRACE(tp) |
---|
21 | #else |
---|
22 | enum { DefaultNumNodes = 10, DefaultExperimentDurSec = 1, DefaultCheckDonePeriod = 2, DefaultExperimentDurOpCount = 20 }; |
---|
23 | static const char * tp_filter |
---|
24 | // = ""; |
---|
25 | // = "+ea"; |
---|
26 | = "*"; |
---|
27 | #define TRACE(tp) \ |
---|
28 | if (strcmp("*", tp_filter) == 0 || strchr(tp_filter, tp)) { \ |
---|
29 | printf("%c", tp); \ |
---|
30 | bobs_report(); \ |
---|
31 | } |
---|
32 | #endif |
---|
33 | |
---|
34 | static B_UserItem *ui = NULL; |
---|
35 | |
---|
36 | static BFX_LISTED_ELEM_T(B_UserItem) *listedItems = NULL; |
---|
37 | static BFX_LISTED_ELEM_T(B_UserItem) observedItem; |
---|
38 | |
---|
39 | static BFX_LIST_HEAD_T(B_UserItem) lst; |
---|
40 | |
---|
41 | |
---|
42 | MAYBE_EXTERN_C ( |
---|
43 | |
---|
44 | volatile size_t bobs_ops_completed = 0; |
---|
45 | volatile unsigned int bobs_prog_inserting = 0; |
---|
46 | volatile unsigned int bobs_prog_removing = 0; |
---|
47 | volatile unsigned int bobs_prog_rollover_flag = 0; |
---|
48 | |
---|
49 | void bobs_seek(unsigned int i) { |
---|
50 | observedItem = listedItems[i]; |
---|
51 | } |
---|
52 | |
---|
53 | void bobs_moveNext() { |
---|
54 | observedItem = BFX_GET_AFTER(B_UserItem, lst, observedItem); |
---|
55 | } |
---|
56 | |
---|
57 | void bobs_movePrev() { |
---|
58 | observedItem = BFX_GET_BEFORE(B_UserItem, lst, observedItem); |
---|
59 | } |
---|
60 | |
---|
61 | int bobs_hasCurrent() { |
---|
62 | return BFX_IS_VALID_POS(B_UserItem, lst, observedItem); |
---|
63 | } |
---|
64 | |
---|
65 | int bobs_getCurrent() { |
---|
66 | B_UserItem * curUI = BFX_DEREF_POS(B_UserItem, lst, observedItem); |
---|
67 | return curUI->userdata[17]; |
---|
68 | } |
---|
69 | |
---|
70 | enum bobs_op_movement_t bobs_op_movement = OP_MOVEMENT; |
---|
71 | enum bobs_op_polarity_t bobs_op_polarity = OP_POLARITY; |
---|
72 | ) |
---|
73 | |
---|
74 | unsigned int uDefaultPreemption() { |
---|
75 | return 0; |
---|
76 | } |
---|
77 | |
---|
78 | int main(int argc, const char *argv[]) { |
---|
79 | |
---|
80 | |
---|
81 | const char * usage_args = "[ExperimentDurSec [CheckDonePeriod [NumNodes [ExperimentDurOpCount]]]]"; |
---|
82 | const int static_arg_posns = 4; |
---|
83 | |
---|
84 | unsigned int ExperimentDurSec = DefaultExperimentDurSec; |
---|
85 | unsigned int CheckDonePeriod = DefaultCheckDonePeriod; |
---|
86 | unsigned int NumNodes = DefaultNumNodes; |
---|
87 | size_t ExperimentDurOpCount = DefaultExperimentDurOpCount; |
---|
88 | |
---|
89 | switch (((argc - 1) < static_arg_posns) ? (argc - 1) : static_arg_posns) { |
---|
90 | case 4: ExperimentDurOpCount = atol(argv[4]); |
---|
91 | case 3: NumNodes = atoi(argv[3]); |
---|
92 | case 2: CheckDonePeriod = atoi(argv[2]); |
---|
93 | case 1: ExperimentDurSec = atoi(argv[1]); |
---|
94 | } |
---|
95 | |
---|
96 | if (ExperimentDurSec == 0 || CheckDonePeriod == 0 || NumNodes == 0 || ExperimentDurOpCount == 0 ) { |
---|
97 | printf("usage: %s %s\n", argv[0], usage_args); |
---|
98 | return -1; |
---|
99 | } |
---|
100 | |
---|
101 | #ifdef DISABLE_CLOCK_RECHECK |
---|
102 | if (ExperimentDurSec != -1) { |
---|
103 | printf("Error: experiment compiled as fixed-work only. ExperimentDurSec (currently %d) must be set to -1.\nUsage: %s %s\n", ExperimentDurSec, argv[0], usage_args); |
---|
104 | return -1; |
---|
105 | } |
---|
106 | #endif |
---|
107 | |
---|
108 | ui = (B_UserItem*) malloc( NumNodes * sizeof(B_UserItem) ); |
---|
109 | memset(ui, 0, NumNodes * sizeof(B_UserItem)); |
---|
110 | |
---|
111 | listedItems = (BFX_LISTED_ELEM_T(B_UserItem)*)malloc( NumNodes * sizeof(BFX_LISTED_ELEM_T(B_UserItem)) ); |
---|
112 | memset(listedItems, 0, NumNodes * sizeof(BFX_LISTED_ELEM_T(B_UserItem))); |
---|
113 | |
---|
114 | for (int i = 0; i < NumNodes; i++) { |
---|
115 | B_UserItem * curUI = & ui[i]; |
---|
116 | curUI->userdata[17] = i; |
---|
117 | } |
---|
118 | |
---|
119 | BFX_INIT(B_UserItem, lst); |
---|
120 | |
---|
121 | bobs_init(NumNodes); |
---|
122 | |
---|
123 | // BOP Convention: |
---|
124 | // Action-number arguments are for the BOP cartridge to interpret. |
---|
125 | // I.e. the driver assumes no relationship between BOP_INSERT(_,_,xx) and ui[xx]. |
---|
126 | // Logical insert number 0 and remove number n-1 are given with a distinguished hook. |
---|
127 | // Logical insert numbers [1,n) and remove numbers [0,n-1) are pumped by the basic SUT hooks. |
---|
128 | // This pattern lets BOP cartridges that measure element-level operations know statically when there is a reference element in the list. |
---|
129 | |
---|
130 | // Default init/teardown is insert/remove |
---|
131 | // Cartridges whose SUT insert/remove actions work on empty lists need not provide special-case ones. |
---|
132 | #ifndef BOP_INIT |
---|
133 | #define BOP_INIT(lst, ui, iters, i) BOP_INSERT(lst, ui, iters, i) |
---|
134 | #endif |
---|
135 | #ifndef BOP_TEARDOWN |
---|
136 | #define BOP_TEARDOWN(lst, ui, iters, i) BOP_REMOVE(lst, ui, iters, i) |
---|
137 | #endif |
---|
138 | |
---|
139 | double elapsed_sec = 0; |
---|
140 | clock_t start = clock(); |
---|
141 | |
---|
142 | size_t privateOpsCompleted = 0; |
---|
143 | |
---|
144 | while (elapsed_sec <= (double) ExperimentDurSec && privateOpsCompleted < ExperimentDurOpCount) { |
---|
145 | for ( int t = 0; t < CheckDonePeriod; t += 1 ) { |
---|
146 | TRACE('a') |
---|
147 | listedItems[0] = |
---|
148 | BOP_INIT(lst, ui, listedItems, 0); |
---|
149 | TRACE('b') |
---|
150 | for ( int privateCurInsert = 1; |
---|
151 | (bobs_prog_inserting = privateCurInsert, privateCurInsert < NumNodes); |
---|
152 | privateCurInsert += 1 |
---|
153 | ) { |
---|
154 | TRACE('-') |
---|
155 | listedItems[privateCurInsert] = |
---|
156 | BOP_INSERT( lst, ui, listedItems, privateCurInsert ); |
---|
157 | TRACE('+') |
---|
158 | } |
---|
159 | TRACE('c') |
---|
160 | for ( int privateCurRemove = 1; |
---|
161 | (bobs_prog_removing = privateCurRemove, privateCurRemove < NumNodes); |
---|
162 | privateCurRemove += 1 |
---|
163 | ) { |
---|
164 | TRACE('-') |
---|
165 | BOP_REMOVE( lst, ui, listedItems, privateCurRemove-1 ); |
---|
166 | TRACE('+') |
---|
167 | } |
---|
168 | TRACE('D') |
---|
169 | BOP_TEARDOWN(lst, ui, listedItems, NumNodes-1); |
---|
170 | TRACE('d') |
---|
171 | |
---|
172 | privateOpsCompleted += NumNodes; |
---|
173 | |
---|
174 | bobs_prog_rollover_flag = 1; |
---|
175 | TRACE('e') |
---|
176 | bobs_prog_inserting = 0; |
---|
177 | bobs_prog_removing = 0; |
---|
178 | bobs_ops_completed = privateOpsCompleted; |
---|
179 | TRACE('f') |
---|
180 | bobs_prog_rollover_flag = 0; |
---|
181 | TRACE('g') |
---|
182 | } |
---|
183 | #ifndef DISABLE_CLOCK_RECHECK |
---|
184 | clock_t end = clock(); |
---|
185 | elapsed_sec = ((double)(end - start)) / ((double)CLOCKS_PER_SEC); |
---|
186 | #endif |
---|
187 | } |
---|
188 | #ifdef DISABLE_CLOCK_RECHECK |
---|
189 | { |
---|
190 | clock_t end = clock(); |
---|
191 | elapsed_sec = ((double)(end - start)) / ((double)CLOCKS_PER_SEC); |
---|
192 | } |
---|
193 | #endif |
---|
194 | |
---|
195 | double mean_op_dur_ns = elapsed_sec / ((double)bobs_ops_completed) * 1000 * 1000 * 1000; |
---|
196 | printf("%s,%zd,%f,%f\n", argv[0], bobs_ops_completed, elapsed_sec, mean_op_dur_ns); |
---|
197 | |
---|
198 | free(ui); |
---|
199 | free(listedItems); |
---|
200 | } |
---|