Rev 550 | 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 | |||
532 | giacomo | 12 | /* OSD Pointer */ |
13 | void *OSD_current_pointer; |
||
497 | giacomo | 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 */ |
||
501 | giacomo | 26 | c->emergency_size = emergency_size; /* Chunk emergency size */ |
498 | giacomo | 27 | c->osd = (DWORD)((DWORD)(c) + sizeof(struct FTrace_Chunk)); |
497 | giacomo | 28 | |
501 | giacomo | 29 | return FTrace_OSD_chunk_init(c, size, emergency_size, flags); |
497 | giacomo | 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 | |||
532 | giacomo | 66 | OSD_current_pointer = NULL; |
497 | giacomo | 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 | |||
532 | giacomo | 117 | int FTrace_chunk_create(int size, int emergency_size, FTrace_flags flags) |
497 | giacomo | 118 | { |
119 | |||
120 | FTrace_Chunk_Ptr FT_temp; |
||
532 | giacomo | 121 | int number, err; |
497 | giacomo | 122 | |
123 | FTrace_lock(); |
||
124 | |||
125 | if (FTraceInit == 0) { |
||
126 | err = FTrace_Init(); |
||
127 | if (err != 0) { |
||
128 | #ifdef FTRACE_DEBUG |
||
129 | FTrace_printf("FTrace Error: Initialization fail\n"); |
||
130 | #endif |
||
131 | FTrace_unlock(); |
||
132 | return -1; |
||
133 | } |
||
134 | } |
||
135 | |||
532 | giacomo | 136 | number = FTrace_find_free_slot(); |
137 | if (number == -1) { |
||
138 | #ifdef FTRACE_DEBUG |
||
139 | FTrace_printf("FTrace Error: cannot find free slot for chunk\n"); |
||
140 | #endif |
||
141 | FTrace_unlock(); |
||
142 | return -1; |
||
143 | } |
||
497 | giacomo | 144 | |
532 | giacomo | 145 | FT_temp = (FTrace_Chunk_Ptr)FTrace_malloc(sizeof(struct FTrace_Chunk) + FTRACE_OSD_CHUNK_HEAD + size + emergency_size); |
146 | if (FT_temp == NULL) { |
||
147 | #ifdef FTRACE_DEBUG |
||
148 | FTrace_printf("FTrace Error: cannot allocate memory for chunk\n"); |
||
149 | #endif |
||
150 | FTrace_unlock(); |
||
151 | return -1; |
||
152 | } |
||
497 | giacomo | 153 | |
532 | giacomo | 154 | memset(FT_temp,0,sizeof(struct FTrace_Chunk) + FTRACE_OSD_CHUNK_HEAD + size + emergency_size); |
497 | giacomo | 155 | |
532 | giacomo | 156 | err = FTrace_chunk_init(FT_temp, number, size, emergency_size, flags); |
157 | if (err != 0) { |
||
158 | #ifdef FTRACE_DEBUG |
||
159 | FTrace_printf("FTrace Error: cannot initialized the new chunk\n"); |
||
160 | #endif |
||
161 | FTrace_unlock(); |
||
162 | return -1; |
||
163 | } |
||
504 | giacomo | 164 | |
532 | giacomo | 165 | /* Set the ChunkTable */ |
166 | ChunkTable[number] = FT_temp; |
||
497 | giacomo | 167 | |
532 | giacomo | 168 | #ifdef FTRACE_DEBUG |
169 | FTrace_printf("FTrace Debug: Chunk %d created at addr %x\n",number,(int)FT_temp); |
||
170 | #endif |
||
497 | giacomo | 171 | |
172 | FTrace_unlock(); |
||
532 | giacomo | 173 | return number; |
497 | giacomo | 174 | |
175 | } |
||
176 | |||
177 | /* Delete a Chunk */ |
||
178 | int FTrace_chunk_delete(int number) |
||
179 | { |
||
180 | |||
181 | FTrace_Chunk_Ptr FT_temp = ChunkTable[number]; |
||
182 | |||
183 | FTrace_lock(); |
||
184 | |||
185 | if (FTraceInit == 0) { |
||
186 | #ifdef FTRACE_DEBUG |
||
187 | FTrace_printf("FTrace Error: FTrace not initialized\n"); |
||
188 | #endif |
||
189 | FTrace_unlock(); |
||
190 | return -1; |
||
191 | } |
||
192 | |||
193 | if (FT_temp == NULL) { |
||
194 | #ifdef FTRACE_DEBUG |
||
195 | FTrace_printf("FTrace Error: Chunk not present\n"); |
||
196 | #endif |
||
197 | FTrace_unlock(); |
||
198 | return -1; |
||
199 | } |
||
200 | |||
201 | if (FT_temp->flags & FTRACE_CHUNK_FLAG_NODEL) { |
||
202 | FTrace_unlock(); |
||
203 | return 0; |
||
204 | } |
||
205 | |||
206 | FTrace_free(FT_temp); |
||
207 | ChunkTable[number] = NULL; |
||
208 | |||
209 | FTrace_unlock(); |
||
210 | return 0; |
||
211 | |||
212 | } |
||
213 | |||
214 | /* Set the chunk flags */ |
||
215 | int FTrace_set_chunk_flags(int number, FTrace_flags flags) |
||
216 | { |
||
217 | |||
218 | FTrace_Chunk_Ptr FT_temp = ChunkTable[number]; |
||
219 | |||
220 | FTrace_lock(); |
||
221 | |||
222 | if (FTraceInit == 0) { |
||
223 | #ifdef FTRACE_DEBUG |
||
224 | FTrace_printf("FTrace Error: FTrace not initialized\n"); |
||
225 | #endif |
||
226 | FTrace_unlock(); |
||
227 | return -1; |
||
228 | } |
||
229 | |||
230 | if (FT_temp == NULL) { |
||
231 | #ifdef FTRACE_DEBUG |
||
232 | FTrace_printf("FTrace Error: Chunk not present\n"); |
||
233 | #endif |
||
234 | FTrace_unlock(); |
||
235 | return -1; |
||
236 | } |
||
237 | |||
238 | FT_temp->flags = flags; |
||
503 | giacomo | 239 | FTrace_OSD_update_chunk_flags(FT_temp); |
497 | giacomo | 240 | |
241 | return 0; |
||
242 | |||
243 | } |
||
244 | |||
245 | /* Get the chunk flags */ |
||
246 | int FTrace_get_chunk_flags(int number, FTrace_flags *flags) |
||
247 | { |
||
248 | |||
249 | FTrace_Chunk_Ptr FT_temp = ChunkTable[number]; |
||
250 | |||
251 | FTrace_lock(); |
||
252 | |||
253 | if (FTraceInit == 0) { |
||
254 | #ifdef FTRACE_DEBUG |
||
255 | FTrace_printf("FTrace Error: FTrace not initialized\n"); |
||
256 | #endif |
||
257 | FTrace_unlock(); |
||
258 | return -1; |
||
259 | } |
||
260 | |||
261 | if (FT_temp == NULL) { |
||
262 | #ifdef FTRACE_DEBUG |
||
263 | FTrace_printf("FTrace Error: Chunk not present\n"); |
||
264 | #endif |
||
265 | FTrace_unlock(); |
||
266 | return -1; |
||
267 | } |
||
532 | giacomo | 268 | |
269 | FTrace_OSD_update_chunk_flags(FT_temp); *flags = FT_temp->flags; |
||
270 | |||
271 | FTrace_unlock(); |
||
272 | |||
497 | giacomo | 273 | return 0; |
274 | |||
275 | } |
||
276 | |||
277 | /* Select the actual chunk */ |
||
278 | int FTrace_actual_chunk_select(int number) |
||
279 | { |
||
280 | |||
281 | FTrace_Chunk_Ptr FT_temp = ChunkTable[number]; |
||
282 | |||
283 | FTrace_lock(); |
||
284 | |||
285 | if (FTraceInit == 0) { |
||
286 | #ifdef FTRACE_DEBUG |
||
287 | FTrace_printf("FTrace Error: FTrace not initialized\n"); |
||
288 | #endif |
||
289 | FTrace_unlock(); |
||
290 | return -1; |
||
291 | } |
||
292 | |||
293 | if (FT_temp == NULL) { |
||
294 | #ifdef FTRACE_DEBUG |
||
295 | FTrace_printf("FTrace Error: Chunk not present\n"); |
||
296 | #endif |
||
297 | FTrace_unlock(); |
||
298 | return -1; |
||
299 | } |
||
300 | |||
301 | if (FT_temp->flags & FTRACE_CHUNK_FLAG_FREE) { |
||
302 | |||
303 | /* Set as used */ |
||
304 | FT_temp->flags &= ~FTRACE_CHUNK_FLAG_FREE; |
||
503 | giacomo | 305 | FTrace_OSD_update_chunk_flags(FT_temp); |
497 | giacomo | 306 | |
307 | /* Update the actual_chunk and OSD_pointers */ |
||
308 | FTrace_fsave(); |
||
309 | ActualChunk = FT_temp; |
||
532 | giacomo | 310 | OSD_current_pointer = (void *)FT_temp->osd; |
497 | giacomo | 311 | FTrace_frestore(); |
312 | |||
313 | } else { |
||
314 | #ifdef FTRACE_DEBUG |
||
315 | FTrace_printf("FTrace Error: Chunk is not free\n"); |
||
316 | #endif |
||
317 | FTrace_unlock(); |
||
318 | return -1; |
||
319 | } |
||
320 | |||
321 | FTrace_unlock(); |
||
322 | |||
323 | return 0; |
||
324 | |||
325 | } |
||
326 | |||
327 | /* Find the first chunk with specific flags*/ |
||
328 | int FTrace_get_first_chunk(FTrace_flags flags) { |
||
329 | |||
330 | int i; |
||
331 | |||
332 | FTrace_lock(); |
||
333 | |||
334 | if (FTraceInit == 0) { |
||
335 | #ifdef FTRACE_DEBUG |
||
336 | FTrace_printf("FTrace Error: FTrace not initialized\n"); |
||
337 | #endif |
||
338 | FTrace_unlock(); |
||
339 | return -1; |
||
340 | } |
||
341 | |||
342 | for (i = 0;i < MAX_CHUNK;i++) { |
||
343 | |||
344 | if (ChunkTable[i]->flags & flags) { |
||
345 | FTrace_unlock(); |
||
346 | return i; |
||
347 | } |
||
348 | |||
349 | } |
||
350 | |||
351 | #ifdef FTRACE_DEBUG |
||
352 | FTrace_printf("FTrace Error: Free chunk not found\n"); |
||
353 | #endif |
||
354 | FTrace_unlock(); |
||
355 | |||
356 | return -1; |
||
357 | |||
358 | } |
||
359 | |||
360 | /* Get chunk table */ |
||
361 | FTrace_Chunk_Ptr *FTrace_get_chunk_table() |
||
362 | { |
||
363 | |||
364 | return ChunkTable; |
||
365 | |||
366 | } |
||
367 | |||
368 | /* Link two chunks */ |
||
532 | giacomo | 369 | int FTrace_chunk_link(int chunk_A, int chunk_B) |
497 | giacomo | 370 | { |
371 | |||
372 | FTrace_Chunk_Ptr ckA = ChunkTable[chunk_A]; |
||
373 | FTrace_Chunk_Ptr ckB = ChunkTable[chunk_B]; |
||
374 | |||
375 | int err; |
||
376 | |||
377 | FTrace_lock(); |
||
378 | |||
379 | if (FTraceInit == 0) { |
||
380 | #ifdef FTRACE_DEBUG |
||
381 | FTrace_printf("FTrace Error: FTrace not initialized\n"); |
||
382 | #endif |
||
383 | FTrace_unlock(); |
||
384 | return -1; |
||
385 | } |
||
386 | |||
387 | if (ckA == NULL) { |
||
388 | #ifdef FTRACE_DEBUG |
||
389 | FTrace_printf("FTrace Error: Chunk A not present\n"); |
||
390 | #endif |
||
391 | FTrace_unlock(); |
||
392 | return -1; |
||
393 | } |
||
394 | |||
395 | if (ckB == NULL) { |
||
396 | #ifdef FTRACE_DEBUG |
||
397 | FTrace_printf("FTrace Error: Chunk B not present\n"); |
||
398 | #endif |
||
399 | FTrace_unlock(); |
||
400 | return -1; |
||
401 | } |
||
402 | |||
403 | if (!(ckB->flags & FTRACE_CHUNK_FLAG_FREE)) { |
||
404 | #ifdef FTRACE_DEBUG |
||
405 | FTrace_printf("FTrace Error: Chunk B is not free\n"); |
||
406 | #endif |
||
407 | FTrace_unlock(); |
||
408 | return -1; |
||
534 | giacomo | 409 | } else { |
410 | ckB->flags &= ~FTRACE_CHUNK_FLAG_FREE; |
||
411 | FTrace_OSD_update_chunk_flags(ckB); |
||
497 | giacomo | 412 | } |
413 | |||
414 | /* FTrace Low Level Blocking Part */ |
||
415 | FTrace_fsave(); |
||
532 | giacomo | 416 | err = FTrace_OSD_chunk_link(ckA,ckB); |
497 | giacomo | 417 | if (err != 0) { |
418 | #ifdef FTRACE_DEBUG |
||
419 | FTrace_printf("FTrace Error: Cannot link the chunks\n"); |
||
420 | #endif |
||
421 | return -1; |
||
422 | } |
||
423 | FTrace_frestore(); |
||
424 | |||
425 | FTrace_unlock(); |
||
426 | |||
427 | return 0; |
||
428 | |||
429 | } |
||
430 | |||
431 | /* Create a new memory region where the compressed data are stored */ |
||
432 | int FTrace_compress_chunk(int number, FTrace_flags new_flags) |
||
433 | { |
||
434 | void *temp_data; |
||
435 | FTrace_Chunk_Ptr FT_temp = ChunkTable[number]; |
||
436 | int err, data_size; |
||
437 | FTrace_Chunk_Ptr New_chunk; |
||
438 | |||
439 | FTrace_lock(); |
||
440 | |||
441 | if (FTraceInit == 0) { |
||
442 | #ifdef FTRACE_DEBUG |
||
443 | FTrace_printf("FTrace Error: FTrace not initialized\n"); |
||
444 | #endif |
||
445 | FTrace_unlock(); |
||
446 | return -1; |
||
447 | } |
||
448 | |||
449 | if (FT_temp == NULL) { |
||
450 | #ifdef FTRACE_DEBUG |
||
451 | FTrace_printf("FTrace Error: Chunk not present\n"); |
||
452 | #endif |
||
453 | FTrace_unlock(); |
||
454 | return -1; |
||
455 | } |
||
456 | |||
457 | FT_temp->flags |= FTRACE_CHUNK_FLAG_BUSY; |
||
552 | giacomo | 458 | FTrace_OSD_update_chunk_flags(FT_temp); |
497 | giacomo | 459 | |
460 | FTrace_unlock(); |
||
461 | |||
462 | /* Alloc temp memory for */ |
||
501 | giacomo | 463 | temp_data = (void *)FTrace_malloc(FT_temp->size+FT_temp->emergency_size); |
497 | giacomo | 464 | if (temp_data == NULL) { |
465 | #ifdef FTRACE_DEBUG |
||
466 | FTrace_printf("FTrace Error: Cannot allocate memory\n"); |
||
467 | #endif |
||
468 | return -1; |
||
469 | } |
||
470 | |||
471 | /* Compress the chunk. Temp_data are a temporary region where |
||
472 | store the compressed chunk. Data_size is the size of compressed |
||
473 | data */ |
||
474 | err = FTrace_OSD_compress_chunk(number,temp_data,&data_size); |
||
475 | if (err != 0) { |
||
476 | #ifdef FTRACE_DEBUG |
||
477 | FTrace_printf("FTrace Error: OSD Compressing function failed\n"); |
||
478 | #endif |
||
479 | return -1; |
||
480 | } |
||
481 | |||
482 | New_chunk = (FTrace_Chunk_Ptr)FTrace_malloc(sizeof(struct FTrace_Chunk) + FTRACE_OSD_CHUNK_HEAD + data_size); |
||
483 | if (New_chunk == NULL) { |
||
484 | #ifdef FTRACE_DEBUG |
||
485 | FTrace_printf("FTrace Error: Cannot allocate memory\n"); |
||
486 | #endif |
||
487 | return -1; |
||
488 | } |
||
489 | |||
490 | memcpy(New_chunk,temp_data,data_size); |
||
491 | |||
492 | FTrace_free(temp_data); |
||
493 | |||
494 | FTrace_lock(); |
||
495 | |||
496 | /* Free the memory of the old chunk */ |
||
497 | FTrace_free(FT_temp); |
||
498 | |||
499 | /* Set the new chunk flags and update the main table */ |
||
500 | New_chunk->flags = new_flags; |
||
501 | New_chunk->flags |= FTRACE_CHUNK_FLAG_COMPR; |
||
552 | giacomo | 502 | FTrace_OSD_update_chunk_flags(New_chunk); |
497 | giacomo | 503 | ChunkTable[number] = New_chunk; |
504 | |||
505 | FTrace_unlock(); |
||
506 | |||
507 | return 0; |
||
508 | |||
509 | } |
||
510 | |||
511 | /* Send the chunk out from the memory */ |
||
512 | int FTrace_send_chunk(int number, int osd_flags, FTrace_flags new_flags) |
||
513 | { |
||
514 | FTrace_Chunk_Ptr FT_temp = ChunkTable[number]; |
||
515 | int err; |
||
516 | |||
517 | FTrace_lock(); |
||
518 | |||
519 | if (FTraceInit == 0) { |
||
520 | #ifdef FTRACE_DEBUG |
||
521 | FTrace_printf("FTrace Error: FTrace not initialized\n"); |
||
522 | #endif |
||
523 | FTrace_unlock(); |
||
524 | return -1; |
||
525 | } |
||
526 | |||
527 | if (FT_temp == NULL) { |
||
528 | #ifdef FTRACE_DEBUG |
||
529 | FTrace_printf("FTrace Error: Chunk not present\n"); |
||
530 | #endif |
||
531 | FTrace_unlock(); |
||
532 | return -1; |
||
533 | } |
||
534 | |||
535 | FT_temp->flags |= FTRACE_CHUNK_FLAG_BUSY; |
||
552 | giacomo | 536 | FTrace_OSD_update_chunk_flags(FT_temp); |
497 | giacomo | 537 | |
538 | FTrace_unlock(); |
||
539 | |||
550 | giacomo | 540 | err = FTrace_OSD_send_chunk(FT_temp, osd_flags); |
497 | giacomo | 541 | if (err != 0) { |
542 | #ifdef FTRACE_DEBUG |
||
543 | FTrace_printf("FTrace Error: Cannot send the chunk\n"); |
||
544 | #endif |
||
545 | return -1; |
||
546 | } |
||
547 | |||
548 | FTrace_lock(); |
||
549 | |||
550 | /* Set the new chunk flags */ |
||
551 | FT_temp->flags = new_flags; |
||
552 | giacomo | 552 | FTrace_OSD_update_chunk_flags(FT_temp); |
497 | giacomo | 553 | |
554 | FTrace_unlock(); |
||
555 | |||
556 | return 0; |
||
557 | |||
558 | } |
||
503 | giacomo | 559 | |
560 | void FTrace_chunk_dump(int number) { |
||
561 | |||
562 | FTrace_Chunk_Ptr FT_temp = ChunkTable[number]; |
||
563 | |||
534 | giacomo | 564 | FTrace_printf("ID : %x\n",FT_temp->id); |
565 | FTrace_printf("NUM : %d\n",FT_temp->number); |
||
566 | FTrace_printf("FLAGS : %x\n",FT_temp->flags); |
||
567 | FTrace_printf("SIZE : %d\n",FT_temp->size); |
||
568 | FTrace_printf("EMERG : %d\n",FT_temp->emergency_size); |
||
504 | giacomo | 569 | |
534 | giacomo | 570 | FTrace_OSD_chunk_dump(FT_temp); |
571 | |||
503 | giacomo | 572 | } |
573 |