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