Rev 497 | Rev 501 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
497 | giacomo | 1 | #include <FTrace_chunk.h> |
2 | #include <FTrace_types.h> |
||
3 | #include <FTrace_OSD.h> |
||
4 | |||
5 | #define FTRACE_DEBUG |
||
6 | |||
7 | /* Globals */ |
||
8 | |||
9 | FTrace_Chunk_Ptr ChunkTable[MAX_CHUNK]; /* Chunk array */ |
||
10 | FTrace_Chunk_Ptr ActualChunk = NULL; /* Actual Chunk */ |
||
11 | |||
12 | /* Global Pointers */ |
||
13 | BYTE OSD_pointers[FTRACE_OSD_CHUNK_HEAD]; |
||
14 | |||
15 | int FTraceInit = 0; |
||
16 | int FTraceEnable = 0; |
||
17 | |||
18 | /* Init a chunk with default value */ |
||
498 | giacomo | 19 | static int FTrace_chunk_init(FTrace_Chunk_Ptr c, int number, int size, int emergency_size, FTrace_flags flags) |
497 | giacomo | 20 | { |
21 | |||
498 | giacomo | 22 | c->id = FTRACE_CHUNK_ID; /* Std ID */ |
23 | c->number = number; /* Chunk number */ |
||
24 | c->flags = flags; /* Chunk flags */ |
||
25 | c->size = size; /* Chunk size */ |
||
26 | c->emergency_size = emergency_size; /* Chunk size */ |
||
27 | c->osd = (DWORD)((DWORD)(c) + sizeof(struct FTrace_Chunk)); |
||
497 | giacomo | 28 | |
29 | return FTrace_OSD_chunk_init(c, size, flags); |
||
30 | |||
31 | } |
||
32 | |||
33 | /* Find a free slot in ChunkTable */ |
||
34 | static int FTrace_find_free_slot() |
||
35 | { |
||
36 | |||
37 | int i; |
||
38 | |||
39 | if (FTraceInit == 0) { |
||
40 | #ifdef FTRACE_DEBUG |
||
41 | FTrace_printf("FTrace Error: FTrace not initialized\n"); |
||
42 | #endif |
||
43 | return -1; |
||
44 | } |
||
45 | |||
46 | for (i = 0;i < MAX_CHUNK;i++) |
||
47 | if (ChunkTable[i] == NULL) |
||
48 | return i; |
||
49 | |||
50 | return -1; |
||
51 | |||
52 | } |
||
53 | |||
54 | /* Init the FTrace */ |
||
55 | static int FTrace_Init() |
||
56 | { |
||
57 | |||
58 | int i,err; |
||
59 | |||
60 | /* Check if it's just initialized */ |
||
61 | if (FTraceInit == 1) |
||
62 | return 0; |
||
63 | |||
64 | FTrace_lock(); |
||
65 | |||
66 | memset(OSD_pointers,0,sizeof(OSD_pointers)); |
||
67 | |||
68 | for (i = 0;i < MAX_CHUNK;i++) |
||
69 | ChunkTable[i] = NULL; |
||
70 | |||
71 | /* Init System Dependet Part */ |
||
72 | err = FTrace_OSD_init(); |
||
73 | if (err != 0) { |
||
74 | #ifdef FTRACE_DEBUG |
||
75 | FTrace_printf("FTrace Error: FTrace OSD not initialized\n"); |
||
76 | #endif |
||
77 | FTrace_unlock(); |
||
78 | return -1; |
||
79 | } |
||
80 | |||
81 | FTraceInit = 1; |
||
82 | |||
83 | FTrace_unlock(); |
||
84 | |||
85 | return 0; |
||
86 | |||
87 | } |
||
88 | |||
89 | /* Enable Tracer */ |
||
90 | int FTrace_enable() |
||
91 | { |
||
92 | |||
93 | if (FTraceInit == 0 || ActualChunk == NULL) return -1; |
||
94 | |||
95 | FTrace_fsave(); |
||
96 | FTraceEnable = 1; |
||
97 | FTrace_frestore(); |
||
98 | |||
99 | return 0; |
||
100 | |||
101 | } |
||
102 | |||
103 | /* Disable Tracer */ |
||
104 | int FTrace_disable() |
||
105 | { |
||
106 | |||
107 | if (FTraceInit == 0) return -1; |
||
108 | |||
109 | FTrace_fsave(); |
||
110 | FTraceEnable = 0; |
||
111 | FTrace_frestore(); |
||
112 | |||
113 | return 0; |
||
114 | |||
115 | } |
||
116 | |||
117 | /* Create n chunks of specified size, where size = 2^bits */ |
||
118 | int FTrace_chunk_create(int n, int bits, FTrace_flags flags) |
||
119 | { |
||
120 | |||
121 | FTrace_Chunk_Ptr FT_temp; |
||
122 | int i, number, err, size; |
||
123 | |||
124 | FTrace_lock(); |
||
125 | |||
126 | if (FTraceInit == 0) { |
||
127 | err = FTrace_Init(); |
||
128 | if (err != 0) { |
||
129 | #ifdef FTRACE_DEBUG |
||
130 | FTrace_printf("FTrace Error: Initialization fail\n"); |
||
131 | #endif |
||
132 | FTrace_unlock(); |
||
133 | return -1; |
||
134 | } |
||
135 | } |
||
136 | |||
137 | /* Get size */ |
||
138 | size = 1 << bits; |
||
139 | |||
140 | for (i = 0;i < n;i++) { |
||
141 | |||
142 | number = FTrace_find_free_slot(); |
||
143 | if (number == -1) { |
||
144 | #ifdef FTRACE_DEBUG |
||
145 | FTrace_printf("FTrace Error: cannot find free slot for chunk\n"); |
||
146 | #endif |
||
147 | FTrace_unlock(); |
||
148 | return -1; |
||
149 | } |
||
150 | |||
151 | FT_temp = (FTrace_Chunk_Ptr)FTrace_malloc(sizeof(struct FTrace_Chunk) + FTRACE_OSD_CHUNK_HEAD + size); |
||
152 | if (FT_temp == NULL) { |
||
153 | #ifdef FTRACE_DEBUG |
||
154 | FTrace_printf("FTrace Error: cannot allocate memory for chunk\n"); |
||
155 | #endif |
||
156 | FTrace_unlock(); |
||
157 | return -1; |
||
158 | } |
||
159 | |||
160 | err = FTrace_chunk_init(FT_temp, number, size, flags); |
||
161 | if (err != 0) { |
||
162 | #ifdef FTRACE_DEBUG |
||
163 | FTrace_printf("FTrace Error: cannot initialized the new chunk\n"); |
||
164 | #endif |
||
165 | FTrace_unlock(); |
||
166 | return -1; |
||
167 | } |
||
168 | |||
169 | /* Set the ChunkTable */ |
||
170 | ChunkTable[number] = FT_temp; |
||
171 | |||
172 | #ifdef FTRACE_DEBUG |
||
173 | FTrace_printf("FTrace Debug: Chunk %d created at addr %x\n",number,(int)FT_temp); |
||
174 | #endif |
||
175 | |||
176 | } |
||
177 | |||
178 | FTrace_unlock(); |
||
179 | return 0; |
||
180 | |||
181 | } |
||
182 | |||
183 | /* Delete a Chunk */ |
||
184 | int FTrace_chunk_delete(int number) |
||
185 | { |
||
186 | |||
187 | FTrace_Chunk_Ptr FT_temp = ChunkTable[number]; |
||
188 | |||
189 | FTrace_lock(); |
||
190 | |||
191 | if (FTraceInit == 0) { |
||
192 | #ifdef FTRACE_DEBUG |
||
193 | FTrace_printf("FTrace Error: FTrace not initialized\n"); |
||
194 | #endif |
||
195 | FTrace_unlock(); |
||
196 | return -1; |
||
197 | } |
||
198 | |||
199 | if (FT_temp == NULL) { |
||
200 | #ifdef FTRACE_DEBUG |
||
201 | FTrace_printf("FTrace Error: Chunk not present\n"); |
||
202 | #endif |
||
203 | FTrace_unlock(); |
||
204 | return -1; |
||
205 | } |
||
206 | |||
207 | if (FT_temp->flags & FTRACE_CHUNK_FLAG_NODEL) { |
||
208 | FTrace_unlock(); |
||
209 | return 0; |
||
210 | } |
||
211 | |||
212 | FTrace_free(FT_temp); |
||
213 | ChunkTable[number] = NULL; |
||
214 | |||
215 | FTrace_unlock(); |
||
216 | return 0; |
||
217 | |||
218 | } |
||
219 | |||
220 | /* Set the chunk flags */ |
||
221 | int FTrace_set_chunk_flags(int number, FTrace_flags flags) |
||
222 | { |
||
223 | |||
224 | FTrace_Chunk_Ptr FT_temp = ChunkTable[number]; |
||
225 | |||
226 | FTrace_lock(); |
||
227 | |||
228 | if (FTraceInit == 0) { |
||
229 | #ifdef FTRACE_DEBUG |
||
230 | FTrace_printf("FTrace Error: FTrace not initialized\n"); |
||
231 | #endif |
||
232 | FTrace_unlock(); |
||
233 | return -1; |
||
234 | } |
||
235 | |||
236 | if (FT_temp == NULL) { |
||
237 | #ifdef FTRACE_DEBUG |
||
238 | FTrace_printf("FTrace Error: Chunk not present\n"); |
||
239 | #endif |
||
240 | FTrace_unlock(); |
||
241 | return -1; |
||
242 | } |
||
243 | |||
244 | FT_temp->flags = flags; |
||
245 | |||
246 | return 0; |
||
247 | |||
248 | } |
||
249 | |||
250 | /* Get the chunk flags */ |
||
251 | int FTrace_get_chunk_flags(int number, FTrace_flags *flags) |
||
252 | { |
||
253 | |||
254 | FTrace_Chunk_Ptr FT_temp = ChunkTable[number]; |
||
255 | |||
256 | FTrace_lock(); |
||
257 | |||
258 | if (FTraceInit == 0) { |
||
259 | #ifdef FTRACE_DEBUG |
||
260 | FTrace_printf("FTrace Error: FTrace not initialized\n"); |
||
261 | #endif |
||
262 | FTrace_unlock(); |
||
263 | return -1; |
||
264 | } |
||
265 | |||
266 | if (FT_temp == NULL) { |
||
267 | #ifdef FTRACE_DEBUG |
||
268 | FTrace_printf("FTrace Error: Chunk not present\n"); |
||
269 | #endif |
||
270 | FTrace_unlock(); |
||
271 | return -1; |
||
272 | } |
||
273 | |||
274 | *flags = FT_temp->flags; |
||
275 | |||
276 | return 0; |
||
277 | |||
278 | } |
||
279 | |||
280 | /* Select the actual chunk */ |
||
281 | int FTrace_actual_chunk_select(int number) |
||
282 | { |
||
283 | |||
284 | FTrace_Chunk_Ptr FT_temp = ChunkTable[number]; |
||
285 | |||
286 | FTrace_lock(); |
||
287 | |||
288 | if (FTraceInit == 0) { |
||
289 | #ifdef FTRACE_DEBUG |
||
290 | FTrace_printf("FTrace Error: FTrace not initialized\n"); |
||
291 | #endif |
||
292 | FTrace_unlock(); |
||
293 | return -1; |
||
294 | } |
||
295 | |||
296 | if (FT_temp == NULL) { |
||
297 | #ifdef FTRACE_DEBUG |
||
298 | FTrace_printf("FTrace Error: Chunk not present\n"); |
||
299 | #endif |
||
300 | FTrace_unlock(); |
||
301 | return -1; |
||
302 | } |
||
303 | |||
304 | if (FT_temp->flags & FTRACE_CHUNK_FLAG_FREE) { |
||
305 | |||
306 | /* Set as used */ |
||
307 | FT_temp->flags &= ~FTRACE_CHUNK_FLAG_FREE; |
||
308 | |||
309 | /* Update the actual_chunk and OSD_pointers */ |
||
310 | FTrace_fsave(); |
||
311 | FTrace_OSD_save_pointers(); |
||
312 | ActualChunk = FT_temp; |
||
313 | FTrace_OSD_load_pointers(); |
||
314 | FTrace_frestore(); |
||
315 | |||
316 | } else { |
||
317 | #ifdef FTRACE_DEBUG |
||
318 | FTrace_printf("FTrace Error: Chunk is not free\n"); |
||
319 | #endif |
||
320 | FTrace_unlock(); |
||
321 | return -1; |
||
322 | } |
||
323 | |||
324 | FTrace_unlock(); |
||
325 | |||
326 | return 0; |
||
327 | |||
328 | } |
||
329 | |||
330 | /* Find the first chunk with specific flags*/ |
||
331 | int FTrace_get_first_chunk(FTrace_flags flags) { |
||
332 | |||
333 | int i; |
||
334 | |||
335 | FTrace_lock(); |
||
336 | |||
337 | if (FTraceInit == 0) { |
||
338 | #ifdef FTRACE_DEBUG |
||
339 | FTrace_printf("FTrace Error: FTrace not initialized\n"); |
||
340 | #endif |
||
341 | FTrace_unlock(); |
||
342 | return -1; |
||
343 | } |
||
344 | |||
345 | for (i = 0;i < MAX_CHUNK;i++) { |
||
346 | |||
347 | if (ChunkTable[i]->flags & flags) { |
||
348 | FTrace_unlock(); |
||
349 | return i; |
||
350 | } |
||
351 | |||
352 | } |
||
353 | |||
354 | #ifdef FTRACE_DEBUG |
||
355 | FTrace_printf("FTrace Error: Free chunk not found\n"); |
||
356 | #endif |
||
357 | FTrace_unlock(); |
||
358 | |||
359 | return -1; |
||
360 | |||
361 | } |
||
362 | |||
363 | /* Get chunk table */ |
||
364 | FTrace_Chunk_Ptr *FTrace_get_chunk_table() |
||
365 | { |
||
366 | |||
367 | return ChunkTable; |
||
368 | |||
369 | } |
||
370 | |||
371 | /* Link two chunks */ |
||
372 | int FTrace_chunk_link(int chunk_A, int chunk_B, int osd_flags) |
||
373 | { |
||
374 | |||
375 | FTrace_Chunk_Ptr ckA = ChunkTable[chunk_A]; |
||
376 | FTrace_Chunk_Ptr ckB = ChunkTable[chunk_B]; |
||
377 | |||
378 | int err; |
||
379 | |||
380 | FTrace_lock(); |
||
381 | |||
382 | if (FTraceInit == 0) { |
||
383 | #ifdef FTRACE_DEBUG |
||
384 | FTrace_printf("FTrace Error: FTrace not initialized\n"); |
||
385 | #endif |
||
386 | FTrace_unlock(); |
||
387 | return -1; |
||
388 | } |
||
389 | |||
390 | if (ckA == NULL) { |
||
391 | #ifdef FTRACE_DEBUG |
||
392 | FTrace_printf("FTrace Error: Chunk A not present\n"); |
||
393 | #endif |
||
394 | FTrace_unlock(); |
||
395 | return -1; |
||
396 | } |
||
397 | |||
398 | if (ckB == NULL) { |
||
399 | #ifdef FTRACE_DEBUG |
||
400 | FTrace_printf("FTrace Error: Chunk B not present\n"); |
||
401 | #endif |
||
402 | FTrace_unlock(); |
||
403 | return -1; |
||
404 | } |
||
405 | |||
406 | if (!(ckB->flags & FTRACE_CHUNK_FLAG_FREE)) { |
||
407 | ckB->flags &= ~FTRACE_CHUNK_FLAG_FREE; |
||
408 | #ifdef FTRACE_DEBUG |
||
409 | FTrace_printf("FTrace Error: Chunk B is not free\n"); |
||
410 | #endif |
||
411 | FTrace_unlock(); |
||
412 | return -1; |
||
413 | } |
||
414 | |||
415 | /* FTrace Low Level Blocking Part */ |
||
416 | FTrace_fsave(); |
||
417 | FTrace_OSD_save_pointers(); |
||
418 | |||
419 | err = FTrace_OSD_chunk_link(ckA,ckB,osd_flags); |
||
420 | if (err != 0) { |
||
421 | #ifdef FTRACE_DEBUG |
||
422 | FTrace_printf("FTrace Error: Cannot link the chunks\n"); |
||
423 | #endif |
||
424 | return -1; |
||
425 | } |
||
426 | |||
427 | FTrace_OSD_load_pointers(); |
||
428 | FTrace_frestore(); |
||
429 | |||
430 | FTrace_unlock(); |
||
431 | |||
432 | return 0; |
||
433 | |||
434 | } |
||
435 | |||
436 | /* Create a new memory region where the compressed data are stored */ |
||
437 | int FTrace_compress_chunk(int number, FTrace_flags new_flags) |
||
438 | { |
||
439 | void *temp_data; |
||
440 | FTrace_Chunk_Ptr FT_temp = ChunkTable[number]; |
||
441 | int err, data_size; |
||
442 | FTrace_Chunk_Ptr New_chunk; |
||
443 | |||
444 | FTrace_lock(); |
||
445 | |||
446 | if (FTraceInit == 0) { |
||
447 | #ifdef FTRACE_DEBUG |
||
448 | FTrace_printf("FTrace Error: FTrace not initialized\n"); |
||
449 | #endif |
||
450 | FTrace_unlock(); |
||
451 | return -1; |
||
452 | } |
||
453 | |||
454 | if (FT_temp == NULL) { |
||
455 | #ifdef FTRACE_DEBUG |
||
456 | FTrace_printf("FTrace Error: Chunk not present\n"); |
||
457 | #endif |
||
458 | FTrace_unlock(); |
||
459 | return -1; |
||
460 | } |
||
461 | |||
462 | FT_temp->flags |= FTRACE_CHUNK_FLAG_BUSY; |
||
463 | |||
464 | FTrace_unlock(); |
||
465 | |||
466 | /* Alloc temp memory for */ |
||
467 | temp_data = (void *)FTrace_malloc(FT_temp->sizemask+1); |
||
468 | if (temp_data == NULL) { |
||
469 | #ifdef FTRACE_DEBUG |
||
470 | FTrace_printf("FTrace Error: Cannot allocate memory\n"); |
||
471 | #endif |
||
472 | return -1; |
||
473 | } |
||
474 | |||
475 | /* Compress the chunk. Temp_data are a temporary region where |
||
476 | store the compressed chunk. Data_size is the size of compressed |
||
477 | data */ |
||
478 | err = FTrace_OSD_compress_chunk(number,temp_data,&data_size); |
||
479 | if (err != 0) { |
||
480 | #ifdef FTRACE_DEBUG |
||
481 | FTrace_printf("FTrace Error: OSD Compressing function failed\n"); |
||
482 | #endif |
||
483 | return -1; |
||
484 | } |
||
485 | |||
486 | New_chunk = (FTrace_Chunk_Ptr)FTrace_malloc(sizeof(struct FTrace_Chunk) + FTRACE_OSD_CHUNK_HEAD + data_size); |
||
487 | if (New_chunk == NULL) { |
||
488 | #ifdef FTRACE_DEBUG |
||
489 | FTrace_printf("FTrace Error: Cannot allocate memory\n"); |
||
490 | #endif |
||
491 | return -1; |
||
492 | } |
||
493 | |||
494 | memcpy(New_chunk,temp_data,data_size); |
||
495 | |||
496 | FTrace_free(temp_data); |
||
497 | |||
498 | FTrace_lock(); |
||
499 | |||
500 | /* Free the memory of the old chunk */ |
||
501 | FTrace_free(FT_temp); |
||
502 | |||
503 | /* Set the new chunk flags and update the main table */ |
||
504 | New_chunk->flags = new_flags; |
||
505 | New_chunk->flags |= FTRACE_CHUNK_FLAG_COMPR; |
||
506 | ChunkTable[number] = New_chunk; |
||
507 | |||
508 | FTrace_unlock(); |
||
509 | |||
510 | return 0; |
||
511 | |||
512 | } |
||
513 | |||
514 | /* Send the chunk out from the memory */ |
||
515 | int FTrace_send_chunk(int number, int osd_flags, FTrace_flags new_flags) |
||
516 | { |
||
517 | FTrace_Chunk_Ptr FT_temp = ChunkTable[number]; |
||
518 | int err; |
||
519 | |||
520 | FTrace_lock(); |
||
521 | |||
522 | if (FTraceInit == 0) { |
||
523 | #ifdef FTRACE_DEBUG |
||
524 | FTrace_printf("FTrace Error: FTrace not initialized\n"); |
||
525 | #endif |
||
526 | FTrace_unlock(); |
||
527 | return -1; |
||
528 | } |
||
529 | |||
530 | if (FT_temp == NULL) { |
||
531 | #ifdef FTRACE_DEBUG |
||
532 | FTrace_printf("FTrace Error: Chunk not present\n"); |
||
533 | #endif |
||
534 | FTrace_unlock(); |
||
535 | return -1; |
||
536 | } |
||
537 | |||
538 | FT_temp->flags |= FTRACE_CHUNK_FLAG_BUSY; |
||
539 | |||
540 | FTrace_unlock(); |
||
541 | |||
542 | err = FTrace_OSD_send_chunk(number, osd_flags); |
||
543 | if (err != 0) { |
||
544 | #ifdef FTRACE_DEBUG |
||
545 | FTrace_printf("FTrace Error: Cannot send the chunk\n"); |
||
546 | #endif |
||
547 | return -1; |
||
548 | } |
||
549 | |||
550 | FTrace_lock(); |
||
551 | |||
552 | /* Set the new chunk flags */ |
||
553 | FT_temp->flags = new_flags; |
||
554 | |||
555 | FTrace_unlock(); |
||
556 | |||
557 | return 0; |
||
558 | |||
559 | } |