Subversion Repositories shark

Rev

Rev 503 | Rev 532 | 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
 
504 giacomo 156
    memset(FT_temp,0,sizeof(struct FTrace_Chunk) + FTRACE_OSD_CHUNK_HEAD + size + emergency_size);
157
 
501 giacomo 158
    err = FTrace_chunk_init(FT_temp, number, size, emergency_size, flags);
497 giacomo 159
    if (err != 0) {
160
      #ifdef FTRACE_DEBUG
161
        FTrace_printf("FTrace Error: cannot initialized the new chunk\n");
162
      #endif
163
      FTrace_unlock();
164
      return -1;
165
    }
166
 
167
    /* Set the ChunkTable */
168
    ChunkTable[number] = FT_temp;
169
 
170
     #ifdef FTRACE_DEBUG
171
       FTrace_printf("FTrace Debug: Chunk %d created at addr %x\n",number,(int)FT_temp);
172
     #endif
173
 
174
  }
175
 
176
  FTrace_unlock();
177
  return 0;
178
 
179
}
180
 
181
/* Delete a Chunk */
182
int FTrace_chunk_delete(int number)
183
{
184
 
185
  FTrace_Chunk_Ptr FT_temp = ChunkTable[number];
186
 
187
  FTrace_lock();
188
 
189
  if (FTraceInit == 0) {
190
    #ifdef FTRACE_DEBUG
191
      FTrace_printf("FTrace Error: FTrace not initialized\n");
192
    #endif
193
    FTrace_unlock();
194
    return -1;
195
  }
196
 
197
  if (FT_temp == NULL) {
198
    #ifdef FTRACE_DEBUG
199
      FTrace_printf("FTrace Error: Chunk not present\n");
200
    #endif
201
    FTrace_unlock();
202
    return -1;
203
  }
204
 
205
  if (FT_temp->flags & FTRACE_CHUNK_FLAG_NODEL) {
206
    FTrace_unlock();
207
    return 0;
208
  }
209
 
210
  FTrace_free(FT_temp);
211
  ChunkTable[number] = NULL;
212
 
213
  FTrace_unlock();
214
  return 0;
215
 
216
}
217
 
218
/* Set the chunk flags */
219
int FTrace_set_chunk_flags(int number, FTrace_flags flags)
220
{
221
 
222
  FTrace_Chunk_Ptr FT_temp = ChunkTable[number];
223
 
224
  FTrace_lock();
225
 
226
  if (FTraceInit == 0) {
227
    #ifdef FTRACE_DEBUG
228
      FTrace_printf("FTrace Error: FTrace not initialized\n");
229
    #endif
230
    FTrace_unlock();
231
    return -1;
232
  }
233
 
234
  if (FT_temp == NULL) {
235
    #ifdef FTRACE_DEBUG
236
      FTrace_printf("FTrace Error: Chunk not present\n");
237
    #endif
238
    FTrace_unlock();
239
    return -1;
240
  }
241
 
242
  FT_temp->flags = flags;
503 giacomo 243
  FTrace_OSD_update_chunk_flags(FT_temp);
497 giacomo 244
 
245
  return 0;
246
 
247
}
248
 
249
/* Get the chunk flags */
250
int FTrace_get_chunk_flags(int number, FTrace_flags *flags)
251
{
252
 
253
  FTrace_Chunk_Ptr FT_temp = ChunkTable[number];
254
 
255
  FTrace_lock();
256
 
257
  if (FTraceInit == 0) {
258
    #ifdef FTRACE_DEBUG
259
      FTrace_printf("FTrace Error: FTrace not initialized\n");
260
    #endif
261
    FTrace_unlock();
262
    return -1;
263
  }
264
 
265
  if (FT_temp == NULL) {
266
    #ifdef FTRACE_DEBUG
267
      FTrace_printf("FTrace Error: Chunk not present\n");
268
    #endif
269
    FTrace_unlock();
270
    return -1;
271
  }
272
 
273
  *flags = FT_temp->flags;
274
 
275
  return 0;
276
 
277
}
278
 
279
/* Select the actual chunk */
280
int FTrace_actual_chunk_select(int number)
281
{
282
 
283
  FTrace_Chunk_Ptr FT_temp = ChunkTable[number];
284
 
285
  FTrace_lock();
286
 
287
  if (FTraceInit == 0) {
288
    #ifdef FTRACE_DEBUG
289
      FTrace_printf("FTrace Error: FTrace not initialized\n");
290
    #endif
291
    FTrace_unlock();
292
    return -1;
293
  }
294
 
295
  if (FT_temp == NULL) {
296
    #ifdef FTRACE_DEBUG
297
      FTrace_printf("FTrace Error: Chunk not present\n");
298
    #endif
299
    FTrace_unlock();
300
    return -1;
301
  }
302
 
303
  if (FT_temp->flags & FTRACE_CHUNK_FLAG_FREE) {
304
 
305
    /* Set as used */
306
    FT_temp->flags &= ~FTRACE_CHUNK_FLAG_FREE;
503 giacomo 307
    FTrace_OSD_update_chunk_flags(FT_temp);
497 giacomo 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;
503 giacomo 408
    FTrace_OSD_update_chunk_flags(ckB);
497 giacomo 409
    #ifdef FTRACE_DEBUG
410
      FTrace_printf("FTrace Error: Chunk B is not free\n");
411
    #endif
412
    FTrace_unlock();
413
    return -1;
414
  }
415
 
416
  /* FTrace Low Level Blocking Part */
417
  FTrace_fsave();
418
    FTrace_OSD_save_pointers();
419
 
420
    err = FTrace_OSD_chunk_link(ckA,ckB,osd_flags);
421
    if (err != 0) {
422
      #ifdef FTRACE_DEBUG
423
        FTrace_printf("FTrace Error: Cannot link the chunks\n");
424
      #endif
425
      return -1;
426
    }
427
 
428
    FTrace_OSD_load_pointers();
429
  FTrace_frestore();
430
 
431
  FTrace_unlock();
432
 
433
  return 0;
434
 
435
}
436
 
504 giacomo 437
/* Update the OSD of actual chunk */
438
int FTrace_update_actual_chunk()
439
{
440
 
441
  FTrace_lock();
442
 
443
  if (ActualChunk == NULL) return 0;
444
 
445
  FTrace_fsave();
446
    FTrace_OSD_save_pointers();
447
  FTrace_frestore();
448
 
449
  FTrace_unlock();
450
 
451
  return 0;
452
 
453
}
454
 
455
 
497 giacomo 456
/* Create a new memory region where the compressed data are stored */
457
int FTrace_compress_chunk(int number, FTrace_flags new_flags)
458
{
459
  void *temp_data;
460
  FTrace_Chunk_Ptr FT_temp = ChunkTable[number];
461
  int err, data_size;
462
  FTrace_Chunk_Ptr New_chunk;
463
 
464
  FTrace_lock();
465
 
466
  if (FTraceInit == 0) {
467
    #ifdef FTRACE_DEBUG
468
      FTrace_printf("FTrace Error: FTrace not initialized\n");
469
    #endif
470
    FTrace_unlock();
471
    return -1;
472
  }
473
 
474
  if (FT_temp == NULL) {
475
    #ifdef FTRACE_DEBUG
476
      FTrace_printf("FTrace Error: Chunk not present\n");
477
    #endif
478
    FTrace_unlock();
479
    return -1;
480
  }
481
 
482
  FT_temp->flags |= FTRACE_CHUNK_FLAG_BUSY;
483
 
484
  FTrace_unlock();
485
 
486
  /* Alloc temp memory for */
501 giacomo 487
  temp_data = (void *)FTrace_malloc(FT_temp->size+FT_temp->emergency_size);
497 giacomo 488
  if (temp_data == NULL) {
489
    #ifdef FTRACE_DEBUG
490
      FTrace_printf("FTrace Error: Cannot allocate memory\n");
491
    #endif
492
    return -1;
493
  }
494
 
495
  /* Compress the chunk. Temp_data are a temporary region where
496
     store the compressed chunk. Data_size is the size of compressed
497
     data */
498
  err = FTrace_OSD_compress_chunk(number,temp_data,&data_size);
499
  if (err != 0) {
500
    #ifdef FTRACE_DEBUG
501
      FTrace_printf("FTrace Error: OSD Compressing function failed\n");
502
    #endif
503
    return -1;
504
  }
505
 
506
  New_chunk = (FTrace_Chunk_Ptr)FTrace_malloc(sizeof(struct FTrace_Chunk) + FTRACE_OSD_CHUNK_HEAD + data_size);
507
  if (New_chunk == NULL) {
508
    #ifdef FTRACE_DEBUG
509
      FTrace_printf("FTrace Error: Cannot allocate memory\n");
510
    #endif
511
    return -1;
512
  }
513
 
514
  memcpy(New_chunk,temp_data,data_size);
515
 
516
  FTrace_free(temp_data);
517
 
518
  FTrace_lock();
519
 
520
  /* Free the memory of the old chunk */
521
  FTrace_free(FT_temp);
522
 
523
  /* Set the new chunk flags and update the main table */
524
  New_chunk->flags = new_flags;
525
  New_chunk->flags |= FTRACE_CHUNK_FLAG_COMPR;
526
  ChunkTable[number] = New_chunk;
527
 
528
  FTrace_unlock();
529
 
530
  return 0;
531
 
532
}
533
 
534
/* Send the chunk out from the memory */
535
int FTrace_send_chunk(int number, int osd_flags, FTrace_flags new_flags)
536
{
537
  FTrace_Chunk_Ptr FT_temp = ChunkTable[number];
538
  int err;                                
539
 
540
  FTrace_lock();
541
 
542
  if (FTraceInit == 0) {
543
    #ifdef FTRACE_DEBUG
544
      FTrace_printf("FTrace Error: FTrace not initialized\n");
545
    #endif
546
    FTrace_unlock();
547
    return -1;
548
  }
549
 
550
  if (FT_temp == NULL) {
551
    #ifdef FTRACE_DEBUG
552
      FTrace_printf("FTrace Error: Chunk not present\n");
553
    #endif
554
    FTrace_unlock();
555
    return -1;
556
  }
557
 
558
  FT_temp->flags |= FTRACE_CHUNK_FLAG_BUSY;
559
 
560
  FTrace_unlock();
561
 
562
  err = FTrace_OSD_send_chunk(number, osd_flags);
563
  if (err != 0) {
564
    #ifdef FTRACE_DEBUG
565
      FTrace_printf("FTrace Error: Cannot send the chunk\n");
566
    #endif
567
    return -1;
568
  }
569
 
570
  FTrace_lock();
571
 
572
  /* Set the new chunk flags */
573
  FT_temp->flags = new_flags;
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];
504 giacomo 584
  int i,t;
585
  DWORD p;
503 giacomo 586
 
504 giacomo 587
  FTrace_update_actual_chunk();
588
 
503 giacomo 589
  cprintf("ID      = %x\n",FT_temp->id);
590
  cprintf("NUM     = %d\n",FT_temp->number);
591
  cprintf("FLAGS   = %d\n",FT_temp->flags);
592
  cprintf("SIZE    = %d\n",FT_temp->size);
593
  cprintf("EMERG   = %d\n",FT_temp->emergency_size);
504 giacomo 594
 
595
  for (i=0; i<0xFF; i++) {
596
    p = *(DWORD *)(FT_temp->osd + 8);
597
    t = 0;
598
    while((p + 16) < *(DWORD *)(FT_temp->osd + 8) + *(DWORD *)(FT_temp->osd + 12)) {
599
      if (*(WORD *)(p+2) == i) t++;
600
      p += 16;
601
    }
602
    if (t != 0)
603
      cprintf("Event type %02x => %d\n",i,t);
604
  }
503 giacomo 605
 
606
}
607
 
608
void FTrace_OSD_dump() {
609
 
610
  void *p = OSD_pointers;
611
  int i;  
612
 
613
  for (i=0;i<FTRACE_OSD_CHUNK_HEAD;i+=4)
614
    cprintf("DATA %d = %x\n",i,*(unsigned int *)(p+i));
615
 
616
}