Subversion Repositories shark

Rev

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