Subversion Repositories shark

Rev

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

Rev Author Line No. Line
1096 pj 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
 *   Paolo Gai           <pj@gandalf.sssup.it>
10
 *   Massimiliano Giorgi <massy@gandalf.sssup.it>
11
 *   Luca Abeni          <luca@gandalf.sssup.it>
12
 *   (see the web pages for full authors list)
13
 *
14
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
15
 *
16
 * http://www.sssup.it
17
 * http://retis.sssup.it
18
 * http://shark.sssup.it
19
 */
20
 
21
/*
22
 * Copyright (C) 2000 Massimiliano Giorgi
23
 * Copyright (C) 2002 Paolo Gai
24
 * Copyright (C) 2002 Tomas Lenvall
25
 *
26
 * This program is free software; you can redistribute it and/or modify
27
 * it under the terms of the GNU General Public License as published by
28
 * the Free Software Foundation; either version 2 of the License, or
29
 * (at your option) any later version.
30
 *
31
 * This program is distributed in the hope that it will be useful,
32
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34
 * GNU General Public License for more details.
35
 *
36
 * You should have received a copy of the GNU General Public License
37
 * along with this program; if not, write to the Free Software
38
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
39
 *
40
 * CVS :        $Id: util.c,v 1.2 2002-10-28 08:07:32 pj Exp $
41
 */
42
 
1093 pj 43
#include <stdio.h>
44
#include <string.h>
45
#include <fcntl.h>
46
#include <unistd.h>
1096 pj 47
#include <stdlib.h>
1093 pj 48
 
49
#include "types.h"
50
#include <trace.h>
51
#include <types.h>
52
 
53
static char *names[]={
54
  "reserved",
55
  "hoops",
56
  /*--*/
57
  "create",
58
  "activate",
59
  "schedule",
60
  "delay",
61
  "sleep",
62
  "endcycle",
63
  "destroy",
64
  "intactiv",
65
  /*--*/
66
  "user0",
67
  "user1",
68
  "user2",
69
  "user3",
70
  "user4",
71
  "user5",
72
  "user6",
73
  "user7",
74
  /*--*/    
75
  "wait",
76
  "waitnb",
77
  "signal"
78
};
79
 
80
int classtable[TRC_NUMCLASSES+1]={
81
  TRC_F_TRACER,
82
  TRC_F_SYSTEM,
83
  TRC_F_USER,
84
  TRC_F_SEM,
85
  TRC_F_LAST
86
};
87
 
88
int event_class(int ev)
89
{
90
  int i;
1096 pj 91
 
92
  if (ev < 0 || ev >= TRC_NUMEVENTS)
93
     return -1;
94
 
95
  for (i = 0; i < TRC_NUMCLASSES; i++)
96
      if (ev >= classtable[i] && ev < classtable[i+1])
97
         return i;
98
 
1093 pj 99
  return -1;
100
}
101
 
102
char *event_name(int ev)
103
{
104
  if (ev<0||ev>=TRC_NUMEVENTS) return "unknown";
105
  return names[ev];
106
}
107
 
108
char *event_hexdump(u_int8_t *ptr,int maxsize)
109
{
110
  static char buffer[256];
111
  int i;
112
  for (i=0;i<maxsize;i++) sprintf(buffer+i*2,"%02x",*(ptr+i));
113
  buffer[maxsize*2]='\0';
114
  return buffer;
115
}
116
 
117
char *event_strdump(u_int8_t *ptr, int maxsize)
118
{
119
  static char buffer[256];
120
  memcpy(buffer,ptr,maxsize);
121
  buffer[maxsize]='\0';
122
  return buffer;
123
}
124
 
125
 
126
char *format_time(long time)
127
{
128
  static char buffer[256];
129
 
130
  if (time<1000l) {
131
    sprintf(buffer,"%li",time);
132
    return buffer;
133
  }
134
  if (time<1000000l) {
135
    sprintf(buffer,"%li,%03li",time/1000l,time%1000l);
136
    return buffer;
137
  }
138
  sprintf(buffer,"%li,%03li,%03li",
139
          (time/1000000l),
140
          (time%1000000l)/1000l,
141
          time%1000l);
142
  return buffer;         
143
}
144
 
145
#ifndef O_BINARY
146
#define O_BINARY 0
147
#endif
148
 
149
int read_trace(char *filename,int (*func)(trc_event_t *))
150
{
151
  trc_event_t buffer;
152
  int fin;
153
  int res;
154
 
155
  fin=open(filename,O_RDONLY|O_BINARY);
156
  if (fin==-1) return -1;
157
  for (;;) {
158
    res=read(fin,&buffer,sizeof(trc_event_t));
159
    if (res!=sizeof(trc_event_t)) {
160
      close(fin);
161
      return res==0?0:-2;
162
    }
163
    res=(*func)(&buffer);
164
    if (res!=0) {
165
      close(fin);
166
      return res;
167
    }
168
  }
169
  close(fin);
170
  return 0;
171
}
1096 pj 172
 
173
 
174
/* reads trace events from a udp message */
175
int read_udp_trace(char *msg, int (*func)(trc_event_t *))
176
{
177
  short int events; // temporary storage of nr of events
178
  int res;
179
 
180
  /* message:
181
     |2 bytes=nr of events|12 bytes=event 0|12 bytes=event 1|...  
182
 
183
     Note: the size of an event depends on the extra informations that
184
     are put on the trc_event_t data structures. That size is
185
     currently 12 bytes, but it can change if additional fields are
186
     added to the trc_event_t structure. Including the
187
     include/trace/types.h header ensures that the size used in this
188
     application is coherent with the size of the compiled Shark
189
     applications...  
190
  */
191
  events = *((short int *)msg);
192
 
193
  msg += 2; // skip the 2 first bytes
194
 
195
  for (;
196
       events > 0;
197
       events--, msg += sizeof(trc_event_t))
198
    res = (*func)((trc_event_t *)msg);
199
 
200
  return 1;
201
}
202