Subversion Repositories shark

Rev

Rev 1063 | Details | Compare with Previous | Last modification | View Log | RSS feed

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