Subversion Repositories shark

Rev

Rev 504 | Rev 534 | 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
    ckB->flags &= ~FTRACE_CHUNK_FLAG_FREE;
503 giacomo 405
    FTrace_OSD_update_chunk_flags(ckB);
497 giacomo 406
    #ifdef FTRACE_DEBUG
407
      FTrace_printf("FTrace Error: Chunk B is not free\n");
408
    #endif
409
    FTrace_unlock();
410
    return -1;
411
  }
412
 
413
  /* FTrace Low Level Blocking Part */
414
  FTrace_fsave();
532 giacomo 415
    err = FTrace_OSD_chunk_link(ckA,ckB);
497 giacomo 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
  FTrace_frestore();
423
 
424
  FTrace_unlock();
425
 
426
  return 0;
427
 
428
}
429
 
430
/* Create a new memory region where the compressed data are stored */
431
int FTrace_compress_chunk(int number, FTrace_flags new_flags)
432
{
433
  void *temp_data;
434
  FTrace_Chunk_Ptr FT_temp = ChunkTable[number];
435
  int err, data_size;
436
  FTrace_Chunk_Ptr New_chunk;
437
 
438
  FTrace_lock();
439
 
440
  if (FTraceInit == 0) {
441
    #ifdef FTRACE_DEBUG
442
      FTrace_printf("FTrace Error: FTrace not initialized\n");
443
    #endif
444
    FTrace_unlock();
445
    return -1;
446
  }
447
 
448
  if (FT_temp == NULL) {
449
    #ifdef FTRACE_DEBUG
450
      FTrace_printf("FTrace Error: Chunk not present\n");
451
    #endif
452
    FTrace_unlock();
453
    return -1;
454
  }
455
 
456
  FT_temp->flags |= FTRACE_CHUNK_FLAG_BUSY;
457
 
458
  FTrace_unlock();
459
 
460
  /* Alloc temp memory for */
501 giacomo 461
  temp_data = (void *)FTrace_malloc(FT_temp->size+FT_temp->emergency_size);
497 giacomo 462
  if (temp_data == NULL) {
463
    #ifdef FTRACE_DEBUG
464
      FTrace_printf("FTrace Error: Cannot allocate memory\n");
465
    #endif
466
    return -1;
467
  }
468
 
469
  /* Compress the chunk. Temp_data are a temporary region where
470
     store the compressed chunk. Data_size is the size of compressed
471
     data */
472
  err = FTrace_OSD_compress_chunk(number,temp_data,&data_size);
473
  if (err != 0) {
474
    #ifdef FTRACE_DEBUG
475
      FTrace_printf("FTrace Error: OSD Compressing function failed\n");
476
    #endif
477
    return -1;
478
  }
479
 
480
  New_chunk = (FTrace_Chunk_Ptr)FTrace_malloc(sizeof(struct FTrace_Chunk) + FTRACE_OSD_CHUNK_HEAD + data_size);
481
  if (New_chunk == NULL) {
482
    #ifdef FTRACE_DEBUG
483
      FTrace_printf("FTrace Error: Cannot allocate memory\n");
484
    #endif
485
    return -1;
486
  }
487
 
488
  memcpy(New_chunk,temp_data,data_size);
489
 
490
  FTrace_free(temp_data);
491
 
492
  FTrace_lock();
493
 
494
  /* Free the memory of the old chunk */
495
  FTrace_free(FT_temp);
496
 
497
  /* Set the new chunk flags and update the main table */
498
  New_chunk->flags = new_flags;
499
  New_chunk->flags |= FTRACE_CHUNK_FLAG_COMPR;
500
  ChunkTable[number] = New_chunk;
501
 
502
  FTrace_unlock();
503
 
504
  return 0;
505
 
506
}
507
 
508
/* Send the chunk out from the memory */
509
int FTrace_send_chunk(int number, int osd_flags, FTrace_flags new_flags)
510
{
511
  FTrace_Chunk_Ptr FT_temp = ChunkTable[number];
512
  int err;                                
513
 
514
  FTrace_lock();
515
 
516
  if (FTraceInit == 0) {
517
    #ifdef FTRACE_DEBUG
518
      FTrace_printf("FTrace Error: FTrace not initialized\n");
519
    #endif
520
    FTrace_unlock();
521
    return -1;
522
  }
523
 
524
  if (FT_temp == NULL) {
525
    #ifdef FTRACE_DEBUG
526
      FTrace_printf("FTrace Error: Chunk not present\n");
527
    #endif
528
    FTrace_unlock();
529
    return -1;
530
  }
531
 
532
  FT_temp->flags |= FTRACE_CHUNK_FLAG_BUSY;
533
 
534
  FTrace_unlock();
535
 
536
  err = FTrace_OSD_send_chunk(number, osd_flags);
537
  if (err != 0) {
538
    #ifdef FTRACE_DEBUG
539
      FTrace_printf("FTrace Error: Cannot send the chunk\n");
540
    #endif
541
    return -1;
542
  }
543
 
544
  FTrace_lock();
545
 
546
  /* Set the new chunk flags */
547
  FT_temp->flags = new_flags;
548
 
549
  FTrace_unlock();
550
 
551
  return 0;
552
 
553
}
503 giacomo 554
 
555
void FTrace_chunk_dump(int number) {
556
 
557
  FTrace_Chunk_Ptr FT_temp = ChunkTable[number];
558
 
559
  cprintf("ID      = %x\n",FT_temp->id);
560
  cprintf("NUM     = %d\n",FT_temp->number);
561
  cprintf("FLAGS   = %d\n",FT_temp->flags);
562
  cprintf("SIZE    = %d\n",FT_temp->size);
563
  cprintf("EMERG   = %d\n",FT_temp->emergency_size);
504 giacomo 564
 
503 giacomo 565
}
566