Subversion Repositories shark

Rev

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

Rev Author Line No. Line
1027 tullio 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
 *   Tullio Facchinetti  <tullio.facchinetti@unipv.it>
10
 *   (see the web pages for full authors list)
11
 *
12
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
13
 *
14
 * http://www.sssup.it
15
 * http://retis.sssup.it
16
 * http://shark.sssup.it
17
 * http://robot.unipv.it
18
 */
19
 
20
/*
21
 * This program is free software; you can redistribute it and/or modify
22
 * it under the terms of the GNU General Public License as published by
23
 * the Free Software Foundation; either version 2 of the License, or
24
 * (at your option) any later version.
25
 *
26
 * This program is distributed in the hope that it will be useful,
27
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29
 * GNU General Public License for more details.
30
 *
31
 * You should have received a copy of the GNU General Public License
32
 * along with this program; if not, write to the Free Software
33
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
34
 */
35
 
36
#include <ll/sys/types.h>
37
#include <stdlib.h>
38
#include <string.h>
39
#include <tracer.h>
40
#include <unistd.h>
41
 
42
#include <FTrace_disk.h>
43
#include <FTrace_OSD.h>
44
#include <FTrace_chunk.h>
45
#include <FTrace_types.h>
46
 
47
#include "ll/i386/x-dos.h"
48
 
49
#include <kernel/kern.h>
50
 
51
#define TRACER_DISK_DEBUG
52
 
53
void write_all_to_disk();
54
 void write_to_disk(FTrace_Chunk_Ptr c, char *my_fname);
55
 
56
int TracerDiskInit = 0;
57
 
58
char fname[255];
59
 
60
/**
61
 * Contains the data of all the chunks that is wanted to write to file.
62
 */
63
FTrace_Chunk_Ptr chunk_to_disk[MAX_CHUNK];
64
 
65
/**
66
 * Keeps track of the number of chunks to be written on disk.
67
 */
68
int n_chunks_to_disk = 0;
69
 
70
/**
71
 * Initialize the Tracer chunk dumper to disk.
72
 * It sets the internal chunk sender to the function that writes
73
 * the chunk on disk. It initializes the filename that will be used
74
 * to open the file for saving the chunk.
75
 */
76
int FTrace_init_disk_writer(char *fn, int flag, char *l_ip, char *t_ip) {
77
        FTrace_set_internal_chunk_sender(FTrace_disk_writer);
78
        memcpy(fname, fn, sizeof(fname));
79
        TracerDiskInit = 1;
80
        return 0;
81
}
82
 
83
/**
84
 * This function is called by the application when it asks to write a chunk on disk.
85
 * It saves the chunk data into the chunk_to_disk array. At the runlevel after the exit,
86
 * all the saved chunks will be written to disk.
87
 */
88
 void FTrace_disk_writer(FTrace_Chunk_Ptr c) {
89
        chunk_to_disk[n_chunks_to_disk++] = c;
90
        sys_atrunlevel(write_all_to_disk, NULL, RUNLEVEL_AFTER_EXIT);
91
}
92
 
93
/**
94
 * This function is invoked at the runlevel after the exit .
95
 * It calls write_to_disk for each chunk that has to be wrote on disk.
96
 * To avoid to use the append flag of the fopen, that seems to not to work properly,
97
 * a new filename is generated for each different chunk (). An incremental number is
98
 * put before the filename and passed to the write_to_disk function.
99
 */
100
void write_all_to_disk() {
101
  int i;
102
  char fname1[225];
103
 
104
  for (i = 0; i < n_chunks_to_disk; i++) {
105
    sprintf(fname1, "%d%s", i, fname);
106
    write_to_disk(chunk_to_disk[i], fname1);
107
  }
108
}
109
 
110
/**
111
 * This function is called by the tracer to write the num-th chunk
112
 * on disk.
113
 */
114
 void write_to_disk(FTrace_Chunk_Ptr c, char *my_fname) {
115
        DWORD total_size = (DWORD)(c->size) + (DWORD)(c->emergency_size);
116
        DWORD start = (DWORD)(c->osd + FTRACE_OSD_CHUNK_HEAD);
117
        DWORD current = start;
118
        int writtenbytes;  /* number of bytes written */
119
        int err;
120
        long count = 0;
121
 
122
        DOS_FILE *f;
123
 
124
        if (!TracerDiskInit) {
125
                cprintf("FTrace_disk_writer: disk writing not initialized.\n");
126
                return;
127
        }
128
 
129
        /* open the DOS file for writing */
130
        f = DOS_fopen(my_fname, "w");
131
 
132
        /* check for open errors */
133
        if (!f) {
134
                /* error!! */
135
                err = DOS_error();
136
 
137
                /* note that if you call DOS_error() here, it return 0!!! */
138
                printk("Error %d opening %s...\n", err, fname);
139
                return;
140
        }
141
 
142
        FTrace_chunck_output_start();
143
 
144
        /** Writes blocks of 16 bytes (a single event) on the disk. */
145
        int block_size = 16;
146
        while (current + block_size <= start + total_size) {
147
                if (*(WORD *)(current) != 0) {
148
                        writtenbytes = DOS_fwrite(current, block_size, 1, f);
149
                        count++;
150
                }
151
                current += block_size;
152
 
153
#ifdef TRACER_DISK_DEBUG
154
        if (!(count % 10))
155
                printk("count %d\n", count);
156
#endif
157
        }
158
 
159
        FTrace_chunck_output_end();
160
 
161
#ifdef TRACER_DISK_DEBUG
162
        printk("Wrote: %d bytes in file %s (count = %ld)\n", total_size, fname, count);
163
        printk("FTrace_OSD_disk_writer: end function\n");
164
#endif
165
 
166
        /* Close the file */
167
        DOS_fclose(f);
168
 
169
        return;
170
}