forked from filebench/filebench
-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc.c
202 lines (170 loc) · 5.19 KB
/
misc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <stdio.h>
#include <fcntl.h>
#include <limits.h>
#include <time.h>
#include <libgen.h>
#include <unistd.h>
#include <strings.h>
#include <sys/time.h>
#include "filebench.h"
#include "ipc.h"
#include "eventgen.h"
#include "utils.h"
#include "fsplug.h"
#include "fbtime.h"
/* File System functions vector */
fsplug_func_t *fs_functions_vec;
extern int lex_lineno;
/*
* Writes a message consisting of information formated by
* "fmt" to the log file, dump file or stdout. The supplied
* "level" argument determines which file to write to and
* what other actions to take.
* The level LOG_DUMP writes to the "dump" file,
* and will open it on the first invocation. Other levels
* print to the stdout device, with the amount of information
* dependent on the error level and the current error level
* setting in filebench_shm->shm_debug_level.
*/
void filebench_log
__V((int level, const char *fmt, ...))
{
va_list args;
hrtime_t now = 0;
char line[131072];
char buf[131072];
/* we want to be able to use filebench_log()
eveing before filebench_shm is initialized.
In this case, all logs have FATAL level. */
if (!filebench_shm)
level = LOG_FATAL;
if (level == LOG_FATAL)
goto fatal;
/* open dumpfile if not already open and writing to it */
if ((level == LOG_DUMP) &&
(*filebench_shm->shm_dump_filename == 0))
return;
if ((level == LOG_DUMP) &&
(filebench_shm->shm_dump_fd < 0)) {
filebench_shm->shm_dump_fd =
open(filebench_shm->shm_dump_filename,
O_RDWR | O_CREAT | O_TRUNC, 0666);
}
if ((level == LOG_DUMP) &&
(filebench_shm->shm_dump_fd < 0)) {
(void) snprintf(line, sizeof (line), "Open logfile failed: %s",
strerror(errno));
level = LOG_ERROR;
}
/* Quit if this is a LOG_ERROR messages and they are disabled */
if ((filebench_shm->shm_1st_err) && (level == LOG_ERROR))
return;
if (level == LOG_ERROR1) {
if (filebench_shm->shm_1st_err)
return;
/* A LOG_ERROR1 temporarily disables LOG_ERROR messages */
filebench_shm->shm_1st_err = 1;
level = LOG_ERROR;
}
/* Only log greater or equal than debug setting */
if ((level != LOG_DUMP) &&
(level > filebench_shm->shm_debug_level))
return;
now = gethrtime();
fatal:
#ifdef __STDC__
va_start(args, fmt);
#else
char *fmt;
va_start(args);
fmt = va_arg(args, char *);
#endif
(void) vsprintf(line, fmt, args);
va_end(args);
if (level == LOG_FATAL) {
(void) fprintf(stderr, "%s\n", line);
return;
}
/* Serialize messages to log */
(void) ipc_mutex_lock(&filebench_shm->shm_msg_lock);
if (level == LOG_DUMP) {
if (filebench_shm->shm_dump_fd != -1) {
(void) snprintf(buf, sizeof (buf), "%s\n", line);
/* We ignore the return value of write() */
if (write(filebench_shm->shm_dump_fd, buf, strlen(buf)));
(void) fsync(filebench_shm->shm_dump_fd);
(void) ipc_mutex_unlock(&filebench_shm->shm_msg_lock);
return;
}
} else if (filebench_shm->shm_debug_level > LOG_INFO) {
if (level < LOG_INFO)
(void) fprintf(stderr, "%5d: ", (int)my_pid);
else
(void) fprintf(stdout, "%5d: ", (int)my_pid);
}
if (level < LOG_INFO) {
(void) fprintf(stderr, "%4.3f: %s",
(now - filebench_shm->shm_epoch) / SEC2NS_FLOAT,
line);
if (my_procflow == NULL)
(void) fprintf(stderr, " around line %d", lex_lineno);
(void) fprintf(stderr, "\n");
(void) fflush(stderr);
} else {
(void) fprintf(stdout, "%4.3f: %s",
(now - filebench_shm->shm_epoch) / SEC2NS_FLOAT,
line);
(void) fprintf(stdout, "\n");
(void) fflush(stdout);
}
(void) ipc_mutex_unlock(&filebench_shm->shm_msg_lock);
}
/*
* Stops the run and exits filebench. If filebench is
* currently running a workload, calls procflow_shutdown()
* to stop the run. Also closes and deletes shared memory.
*/
void
filebench_shutdown(int error) {
if (error) {
filebench_log(LOG_DEBUG_IMPL, "Shutdown on error %d", error);
(void) ipc_mutex_lock(&filebench_shm->shm_procflow_lock);
if (filebench_shm->shm_f_abort == FILEBENCH_ABORT_FINI) {
(void) ipc_mutex_unlock(
&filebench_shm->shm_procflow_lock);
return;
}
filebench_shm->shm_f_abort = FILEBENCH_ABORT_ERROR;
(void) ipc_mutex_unlock(&filebench_shm->shm_procflow_lock);
} else {
filebench_log(LOG_DEBUG_IMPL, "Shutdown");
}
procflow_shutdown();
(void) unlink("/tmp/filebench_shm");
ipc_ismdelete();
exit(error);
}