@@ -34,9 +34,11 @@ extern int debug_file_reopen(void);
34
34
extern int debug_file_close (void );
35
35
36
36
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 ;
38
38
static char debug_program_name [PATH_MAX ];
39
39
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 ;
40
42
41
43
struct flag_info {
42
44
const char * name ;
@@ -182,20 +184,37 @@ static void do_debug(int64_t flags, const char *fmt, va_list args)
182
184
gettimeofday (& tv , 0 );
183
185
tm = localtime (& tv .tv_sec );
184
186
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
+
185
203
buffer_putfstring (& B ,
186
- "%04d/%02d/%02d %02d:%02d:%02d.%02ld " ,
204
+ "%04d/%02d/%02d %02d:%02d:%02d.%02ld %s[%d] " ,
187
205
tm -> tm_year + 1900 ,
188
206
tm -> tm_mon + 1 ,
189
207
tm -> tm_mday ,
190
208
tm -> tm_hour ,
191
209
tm -> tm_min ,
192
210
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 );
195
214
}
196
215
/* 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 ());
199
218
}
200
219
buffer_putfstring (& B , "%s: " , debug_flags_to_name (flags ));
201
220
@@ -309,16 +328,17 @@ void debug_config_file(const char *path)
309
328
void debug_config (const char * name )
310
329
{
311
330
strncpy (debug_program_name , path_basename (name ), sizeof (debug_program_name ) - 1 );
331
+ debug_cached_pid = getpid ();
312
332
}
313
333
314
334
void debug_config_file_size (off_t size )
315
335
{
316
336
debug_file_size (size );
317
337
}
318
338
319
- void debug_config_getpid (pid_t (* getpidf )(void ))
339
+ void debug_config_child_getpid (pid_t (* getpidf )(void ))
320
340
{
321
- debug_getpid = getpidf ;
341
+ debug_child_getpid = getpidf ;
322
342
}
323
343
324
344
int64_t debug_flags_clear ()
0 commit comments