Skip to content

Commit abb440b

Browse files
authored
Debug: Remove Excess System Calls (#4052)
* - Modify debug() to avoid making excess system calls to getpid() and localtime() for every debug event. - Rename debug_getpid -> debug_child_getpid to clarify purpose for sole use in parrot. * format
1 parent 9e0e60a commit abb440b

File tree

3 files changed

+31
-11
lines changed

3 files changed

+31
-11
lines changed

dttools/src/debug.c

+28-8
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@ extern int debug_file_reopen(void);
3434
extern int debug_file_close(void);
3535

3636
static void (*debug_write)(int64_t flags, const char *str) = debug_stderr_write;
37-
static pid_t (*debug_getpid)(void) = getpid;
37+
static pid_t (*debug_child_getpid)(void) = 0;
3838
static char debug_program_name[PATH_MAX];
3939
static int64_t debug_flags = D_NOTICE | D_ERROR | D_FATAL;
40+
static pid_t debug_cached_pid = 0;
41+
static int debug_time_zone_cached = 0;
4042

4143
struct flag_info {
4244
const char *name;
@@ -182,20 +184,37 @@ static void do_debug(int64_t flags, const char *fmt, va_list args)
182184
gettimeofday(&tv, 0);
183185
tm = localtime(&tv.tv_sec);
184186

187+
/*
188+
If the TZ environment variable is not set, then every single call
189+
to localtime() results in a stat("/etc/localtime") which impacts
190+
the minimum latency of a debug event.
191+
*/
192+
193+
if (!debug_time_zone_cached) {
194+
if (!getenv("TZ")) {
195+
setenv("TZ", tm->tm_zone, 0);
196+
}
197+
debug_time_zone_cached = 1;
198+
}
199+
200+
/* Fetch the pid just once and use it multiple times. */
201+
pid_t pid = getpid();
202+
185203
buffer_putfstring(&B,
186-
"%04d/%02d/%02d %02d:%02d:%02d.%02ld ",
204+
"%04d/%02d/%02d %02d:%02d:%02d.%02ld %s[%d]",
187205
tm->tm_year + 1900,
188206
tm->tm_mon + 1,
189207
tm->tm_mday,
190208
tm->tm_hour,
191209
tm->tm_min,
192210
tm->tm_sec,
193-
(long)tv.tv_usec / 10000);
194-
buffer_putfstring(&B, "%s[%d] ", debug_program_name, getpid());
211+
(long)tv.tv_usec / 10000,
212+
debug_program_name,
213+
pid);
195214
}
196215
/* Parrot prints debug messages for children: */
197-
if (getpid() != debug_getpid()) {
198-
buffer_putfstring(&B, "<child:%d> ", (int)debug_getpid());
216+
if (debug_child_getpid) {
217+
buffer_putfstring(&B, "<child:%d> ", (int)debug_child_getpid());
199218
}
200219
buffer_putfstring(&B, "%s: ", debug_flags_to_name(flags));
201220

@@ -309,16 +328,17 @@ void debug_config_file(const char *path)
309328
void debug_config(const char *name)
310329
{
311330
strncpy(debug_program_name, path_basename(name), sizeof(debug_program_name) - 1);
331+
debug_cached_pid = getpid();
312332
}
313333

314334
void debug_config_file_size(off_t size)
315335
{
316336
debug_file_size(size);
317337
}
318338

319-
void debug_config_getpid(pid_t (*getpidf)(void))
339+
void debug_config_child_getpid(pid_t (*getpidf)(void))
320340
{
321-
debug_getpid = getpidf;
341+
debug_child_getpid = getpidf;
322342
}
323343

324344
int64_t debug_flags_clear()

dttools/src/debug.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ modify the linker namespace we are using.
124124
#define debug_config_file cctools_debug_config_file
125125
#define debug_config_file_size cctools_debug_config_file_size
126126
#define debug_config_fatal cctools_debug_config_fatal
127-
#define debug_config_getpid cctools_debug_config_getpid
127+
#define debug_config_child_getpid cctools_debug_config_child_getpid
128128
#define debug_flags_set cctools_debug_flags_set
129129
#define debug_flags_print cctools_debug_flags_print
130130
#define debug_flags_clear cctools_debug_flags_clear
@@ -206,7 +206,7 @@ void debug_config_file_size(off_t size);
206206

207207
void debug_config_fatal(void (*callback) (void));
208208

209-
void debug_config_getpid (pid_t (*getpidf)(void));
209+
void debug_config_child_getpid (pid_t (*getpidf)(void));
210210

211211
/** Set debugging flags to enable output.
212212
Accepts a debug flag in ASCII form, and enables that subsystem. For example: <tt>debug_flags_set("chirp");</tt>

parrot/src/pfs_main.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ int main( int argc, char *argv[] )
612612
debug_config(argv[0]);
613613
debug_config_file_size(0); /* do not rotate debug file by default */
614614
debug_config_fatal(pfs_process_killall);
615-
debug_config_getpid(pfs_process_getpid);
615+
debug_config_child_getpid(pfs_process_getpid);
616616

617617
/* Special file descriptors (currently the channel and the Parrot
618618
* directory) are allocated from the top of our file descriptor pool. After

0 commit comments

Comments
 (0)