Subversion Repositories shark

Rev

Rev 364 | Rev 368 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
352 giacomo 1
/*
2
 * Project: S.Ha.R.K.
3
 *
4
 * Coordinators:
5
 *   Giorgio Buttazzo    <giorgio@sssup.it>
6
 *   Paolo Gai           <pj@gandalf.sssup.it>
7
 *
8
 * Authors     :
9
 *   Giacomo Guidi       <giacomo@gandalf.sssup.it>
10
 *
11
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
12
 *
13
 * http://www.sssup.it
14
 * http://retis.sssup.it
15
 * http://shark.sssup.it
16
 */
17
 
18
#ifndef __TRACER_H__
19
#define __TRACER_H__
20
 
21
#ifdef __OLD_TRACER__
22
 
23
#include <ll/sys/types.h>
24
#include "types.h"
25
#include "trace.h"
355 giacomo 26
#include "FTrace.h"
352 giacomo 27
 
28
#define TRACER_LOGEVENT oldtrace
29
 
30
extern int (*trc_register_eventclass)
31
     (int class,int num,int(*func)(trc_event_t *, int event, void *ptr));
32
 
33
extern void (*trc_logevent)(int event, void *ptr);
34
 
35
extern int (*trc_resume)(void);
36
extern int (*trc_suspend)(void);
37
 
38
#define NOT_DEFINED 0xFF
39
 
40
extern __inline__ void oldtrace(BYTE type, BYTE flag, DWORD par1, DWORD par2)
41
{
42
 
43
  static int oldtype;
44
  static int oldpar;
45
  extern BYTE conv[256]; // Conversion table: ctable.c
46
  BYTE ttype = 0;
47
 
48
  ttype = type & 0x0F;
49
  type = type >> 4;
50
  type |= (ttype << 4);
51
 
52
  oldtype = conv[type];
53
 
54
  if (oldtype != NOT_DEFINED) trc_logevent(oldtype,&oldpar);
55
 
56
}
57
 
58
 
59
#else
60
#ifdef __NEW_TRACER__
61
 
62
#include <ll/sys/types.h>
63
#include <ll/i386/hw-instr.h>
64
#include "FTrace.h"
65
 
364 giacomo 66
#define TRACER_NO_OUTPUT  0
67
#define TRACER_UDP_OUTPUT 1
68
 
352 giacomo 69
#define TRACER_LOGEVENT fast_logevent
70
 
365 giacomo 71
int tracer_initialize(int MemorySize);
362 giacomo 72
void tracer_enable();
73
void tracer_disable();
74
void tracer_print_statistics();
75
 
365 giacomo 76
//UDP
77
 
78
int init_udp_tracer(char *local_ip, char *target_ip);
79
 
352 giacomo 80
#define TRACER_SERIALIZE
81
 
82
/* Save bytes => 1(type) + 4(tsc_high) + 4(tsc_low) + 1(size)
83
                if (flag & 1) + 4(par1)
84
                if (flag & 3) + 4(par2)
85
 * Return 0 -> Success
86
 * Return 1 -> Inactive
87
 * Return 2 -> Error
88
 */
89
extern __inline__ int fast_logevent(BYTE type, BYTE flag, DWORD par1, DWORD par2)
90
{
91
 
92
  extern void *StartTracerBuffer;
93
  extern void *EndTracerBuffer;
362 giacomo 94
  extern void *LastBeforeEndTracerBuffer;
352 giacomo 95
  extern void *CurrentTracerBuffer;
362 giacomo 96
  extern void *FirstTracerBuffer;
97
 
352 giacomo 98
  extern int TracerActive;
99
  extern unsigned long long TracerEventsRecorded;
364 giacomo 100
  extern unsigned int TracerEventsSaved;
352 giacomo 101
 
102
  SYS_FLAGS f;
103
 
104
  DWORD tsc_low, tsc_high;
105
  BYTE size = 10;
106
 
107
  f = ll_fsave();
108
 
109
  if (!TracerActive) {
110
    ll_frestore(f);
111
    return 1;
112
  }
113
 
114
  #ifdef TRACER_SERIALIZE
115
    __asm__("xorl %%eax,%%eax\n\t"
116
            "cpuid\n\t"
117
            "rdtsc\n\t"
118
            :"=a" (tsc_low), "=d" (tsc_high)
119
            :
120
            :"ebx","ecx");
121
  #else
122
    __asm__("rdtsc\n\t"
123
            :"=a" (tsc_low), "=d" (tsc_high)
124
            ::);
125
  #endif
126
 
127
  if (flag & 1) size += 4;
128
  if (flag & 3) size += 4;
129
 
362 giacomo 130
  // Adjust FirstTracerBuffer
364 giacomo 131
  if (FirstTracerBuffer >= CurrentTracerBuffer && TracerEventsSaved != 0) {
363 giacomo 132
    while ((CurrentTracerBuffer + size - 1) >= FirstTracerBuffer &&
133
            FirstTracerBuffer != StartTracerBuffer) {
362 giacomo 134
      FirstTracerBuffer += *(BYTE *)(FirstTracerBuffer + 9);
363 giacomo 135
      if (FirstTracerBuffer >= LastBeforeEndTracerBuffer) FirstTracerBuffer = StartTracerBuffer;
362 giacomo 136
    }
364 giacomo 137
  } else {
138
    TracerEventsSaved++;
362 giacomo 139
  }
140
 
141
  // Check if we overcome EndTracerBuffer
352 giacomo 142
  if ((CurrentTracerBuffer + size - 1) > EndTracerBuffer) {
143
    //Cyclical Buffer implementation
362 giacomo 144
    LastBeforeEndTracerBuffer = CurrentTracerBuffer;
352 giacomo 145
    CurrentTracerBuffer = StartTracerBuffer;
362 giacomo 146
    //Set the First Event
363 giacomo 147
    while ((CurrentTracerBuffer + size - 1) >= FirstTracerBuffer) {
148
      FirstTracerBuffer += *(BYTE *)(FirstTracerBuffer + 9);
149
      if (FirstTracerBuffer >= LastBeforeEndTracerBuffer) FirstTracerBuffer = StartTracerBuffer;
150
    }
352 giacomo 151
  }
152
 
362 giacomo 153
  // Add the new event
352 giacomo 154
  *(BYTE *)CurrentTracerBuffer = type;
155
  CurrentTracerBuffer++;
156
  *(DWORD *)(CurrentTracerBuffer) = tsc_high;
157
  CurrentTracerBuffer+=4;
158
  *(DWORD *)(CurrentTracerBuffer) = tsc_low;
159
  CurrentTracerBuffer+=4;
160
  *(BYTE *)(CurrentTracerBuffer) = size;
161
  CurrentTracerBuffer++;
162
  if (flag & 1) {
163
    *(DWORD *)(CurrentTracerBuffer) = par1;
164
    CurrentTracerBuffer+=4;
165
  }
166
  if (flag & 3) {
167
    *(DWORD *)(CurrentTracerBuffer) = par2;
168
    CurrentTracerBuffer+=4;
169
  }
170
 
171
  if (CurrentTracerBuffer > EndTracerBuffer) {
172
    ll_frestore(f);
173
    return 2;
174
  }
175
 
176
  TracerEventsRecorded++;
177
 
178
  ll_frestore(f);
179
  return 0;
180
 
181
}
182
 
183
#else
184
 
185
#define TRACER_LOGEVENT(a,b,c,d)
186
 
187
#endif
188
 
189
#endif
190
 
191
#endif