Subversion Repositories shark

Rev

Rev 501 | Rev 504 | 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;
503 giacomo 241
  FTrace_OSD_update_chunk_flags(FT_temp);
497 giacomo 242
 
243
  return 0;
244
 
245
}
246
 
247
/* Get the chunk flags */
248
int FTrace_get_chunk_flags(int number, FTrace_flags *flags)
249
{
250
 
251
  FTrace_Chunk_Ptr FT_temp = ChunkTable[number];
252
 
253
  FTrace_lock();
254
 
255
  if (FTraceInit == 0) {
256
    #ifdef FTRACE_DEBUG
257
      FTrace_printf("FTrace Error: FTrace not initialized\n");
258
    #endif
259
    FTrace_unlock();
260
    return -1;
261
  }
262
 
263
  if (FT_temp == NULL) {
264
    #ifdef FTRACE_DEBUG
265
      FTrace_printf("FTrace Error: Chunk not present\n");
266
    #endif
267
    FTrace_unlock();
268
    return -1;
269
  }
270
 
271
  *flags = FT_temp->flags;
272
 
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
      FTrace_OSD_save_pointers();
310
      ActualChunk = FT_temp;
311
      FTrace_OSD_load_pointers();
312
    FTrace_frestore();
313
 
314
  } else {
315
    #ifdef FTRACE_DEBUG
316
      FTrace_printf("FTrace Error: Chunk is not free\n");
317
    #endif
318
    FTrace_unlock();
319
    return -1;
320
  }
321
 
322
  FTrace_unlock();
323
 
324
  return 0;
325
 
326
}
327
 
328
/* Find the first chunk with specific flags*/
329
int FTrace_get_first_chunk(FTrace_flags flags) {
330
 
331
  int i;
332
 
333
  FTrace_lock();
334
 
335
  if (FTraceInit == 0) {
336
    #ifdef FTRACE_DEBUG
337
      FTrace_printf("FTrace Error: FTrace not initialized\n");
338
    #endif
339
    FTrace_unlock();
340
    return -1;
341
  }
342
 
343
  for (i = 0;i < MAX_CHUNK;i++) {
344
 
345
    if (ChunkTable[i]->flags & flags) {
346
      FTrace_unlock();
347
      return i;
348
    }
349
 
350
  }
351
 
352
 #ifdef FTRACE_DEBUG
353
   FTrace_printf("FTrace Error: Free chunk not found\n");
354
 #endif
355
 FTrace_unlock();
356
 
357
 return -1;
358
 
359
}
360
 
361
/* Get chunk table */
362
FTrace_Chunk_Ptr *FTrace_get_chunk_table()
363
{
364
 
365
  return ChunkTable;
366
 
367
}
368
 
369
/* Link two chunks */
370
int FTrace_chunk_link(int chunk_A, int chunk_B, int osd_flags)
371
{
372
 
373
  FTrace_Chunk_Ptr ckA = ChunkTable[chunk_A];
374
  FTrace_Chunk_Ptr ckB = ChunkTable[chunk_B];
375
 
376
  int err;
377
 
378
  FTrace_lock();
379
 
380
  if (FTraceInit == 0) {
381
    #ifdef FTRACE_DEBUG
382
      FTrace_printf("FTrace Error: FTrace not initialized\n");
383
    #endif
384
    FTrace_unlock();
385
    return -1;
386
  }
387
 
388
  if (ckA == NULL) {
389
    #ifdef FTRACE_DEBUG
390
      FTrace_printf("FTrace Error: Chunk A not present\n");
391
    #endif
392
    FTrace_unlock();
393
    return -1;
394
  }
395
 
396
  if (ckB == NULL) {
397
    #ifdef FTRACE_DEBUG
398
      FTrace_printf("FTrace Error: Chunk B not present\n");
399
    #endif
400
    FTrace_unlock();
401
    return -1;
402
  }
403
 
404
  if (!(ckB->flags & FTRACE_CHUNK_FLAG_FREE)) {
405
    ckB->flags &= ~FTRACE_CHUNK_FLAG_FREE;
503 giacomo 406
    FTrace_OSD_update_chunk_flags(ckB);
497 giacomo 407
    #ifdef FTRACE_DEBUG
408
      FTrace_printf("FTrace Error: Chunk B is not free\n");
409
    #endif
410
    FTrace_unlock();
411
    return -1;
412
  }
413
 
414
  /* FTrace Low Level Blocking Part */
415
  FTrace_fsave();
416
    FTrace_OSD_save_pointers();
417
 
418
    err = FTrace_OSD_chunk_link(ckA,ckB,osd_flags);
419
    if (err != 0) {
420
      #ifdef FTRACE_DEBUG
421
        FTrace_printf("FTrace Error: Cannot link the chunks\n");
422
      #endif
423
      return -1;
424
    }
425
 
426
    FTrace_OSD_load_pointers();
427
  FTrace_frestore();
428
 
429
  FTrace_unlock();
430
 
431
  return 0;
432
 
433
}
434
 
435
/* Create a new memory region where the compressed data are stored */
436
int FTrace_compress_chunk(int number, FTrace_flags new_flags)
437
{
438
  void *temp_data;
439
  FTrace_Chunk_Ptr FT_temp = ChunkTable[number];
440
  int err, data_size;
441
  FTrace_Chunk_Ptr New_chunk;
442
 
443
  FTrace_lock();
444
 
445
  if (FTraceInit == 0) {
446
    #ifdef FTRACE_DEBUG
447
      FTrace_printf("FTrace Error: FTrace not initialized\n");
448
    #endif
449
    FTrace_unlock();
450
    return -1;
451
  }
452
 
453
  if (FT_temp == NULL) {
454
    #ifdef FTRACE_DEBUG
455
      FTrace_printf("FTrace Error: Chunk not present\n");
456
    #endif
457
    FTrace_unlock();
458
    return -1;
459
  }
460
 
461
  FT_temp->flags |= FTRACE_CHUNK_FLAG_BUSY;
462
 
463
  FTrace_unlock();
464
 
465
  /* Alloc temp memory for */
501 giacomo 466
  temp_data = (void *)FTrace_malloc(FT_temp->size+FT_temp->emergency_size);
497 giacomo 467
  if (temp_data == NULL) {
468
    #ifdef FTRACE_DEBUG
469
      FTrace_printf("FTrace Error: Cannot allocate memory\n");
470
    #endif
471
    return -1;
472
  }
473
 
474
  /* Compress the chunk. Temp_data are a temporary region where
475
     store the compressed chunk. Data_size is the size of compressed
476
     data */
477
  err = FTrace_OSD_compress_chunk(number,temp_data,&data_size);
478
  if (err != 0) {
479
    #ifdef FTRACE_DEBUG
480
      FTrace_printf("FTrace Error: OSD Compressing function failed\n");
481
    #endif
482
    return -1;
483
  }
484
 
485
  New_chunk = (FTrace_Chunk_Ptr)FTrace_malloc(sizeof(struct FTrace_Chunk) + FTRACE_OSD_CHUNK_HEAD + data_size);
486
  if (New_chunk == NULL) {
487
    #ifdef FTRACE_DEBUG
488
      FTrace_printf("FTrace Error: Cannot allocate memory\n");
489
    #endif
490
    return -1;
491
  }
492
 
493
  memcpy(New_chunk,temp_data,data_size);
494
 
495
  FTrace_free(temp_data);
496
 
497
  FTrace_lock();
498
 
499
  /* Free the memory of the old chunk */
500
  FTrace_free(FT_temp);
501
 
502
  /* Set the new chunk flags and update the main table */
503
  New_chunk->flags = new_flags;
504
  New_chunk->flags |= FTRACE_CHUNK_FLAG_COMPR;
505
  ChunkTable[number] = New_chunk;
506
 
507
  FTrace_unlock();
508
 
509
  return 0;
510
 
511
}
512
 
513
/* Send the chunk out from the memory */
514
int FTrace_send_chunk(int number, int osd_flags, FTrace_flags new_flags)
515
{
516
  FTrace_Chunk_Ptr FT_temp = ChunkTable[number];
517
  int err;                                
518
 
519
  FTrace_lock();
520
 
521
  if (FTraceInit == 0) {
522
    #ifdef FTRACE_DEBUG
523
      FTrace_printf("FTrace Error: FTrace not initialized\n");
524
    #endif
525
    FTrace_unlock();
526
    return -1;
527
  }
528
 
529
  if (FT_temp == NULL) {
530
    #ifdef FTRACE_DEBUG
531
      FTrace_printf("FTrace Error: Chunk not present\n");
532
    #endif
533
    FTrace_unlock();
534
    return -1;
535
  }
536
 
537
  FT_temp->flags |= FTRACE_CHUNK_FLAG_BUSY;
538
 
539
  FTrace_unlock();
540
 
541
  err = FTrace_OSD_send_chunk(number, osd_flags);
542
  if (err != 0) {
543
    #ifdef FTRACE_DEBUG
544
      FTrace_printf("FTrace Error: Cannot send the chunk\n");
545
    #endif
546
    return -1;
547
  }
548
 
549
  FTrace_lock();
550
 
551
  /* Set the new chunk flags */
552
  FT_temp->flags = new_flags;
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
 
564
  cprintf("ID      = %x\n",FT_temp->id);
565
  cprintf("NUM     = %d\n",FT_temp->number);
566
  cprintf("FLAGS   = %d\n",FT_temp->flags);
567
  cprintf("SIZE    = %d\n",FT_temp->size);
568
  cprintf("EMERG   = %d\n",FT_temp->emergency_size);
569
 
570
}
571
 
572
void FTrace_OSD_dump() {
573
 
574
  void *p = OSD_pointers;
575
  int i;  
576
 
577
  for (i=0;i<FTRACE_OSD_CHUNK_HEAD;i+=4)
578
    cprintf("DATA %d = %x\n",i,*(unsigned int *)(p+i));
579
 
580
}