[perl-devel-nytprof commit] r111 - branches/tim.bunce

0 views
Skip to first unread message

codesite...@google.com

unread,
Jun 9, 2008, 7:18:43 PM6/9/08
to develnyt...@googlegroups.com
Author: tim.bunce
Date: Mon Jun 9 16:17:54 2008
New Revision: 111

Modified:
branches/tim.bunce/NYTProf.xs

Log:
Fix STDERR to mean STDERR.
Some general refactoring working towards open-new-file-on-fork...
Rename init_runtime to set_options_from_env.
Move print_header call into open_output_file.
open_output_file now croaks itself on failure.
Move fork checking into new check_if_forked sub.
Unify error handling for opening STDOUT & STDERR (still messy).
Sketch out some rough open-new-file-on-fork logic.
All tests pass.


Modified: branches/tim.bunce/NYTProf.xs
==============================================================================
--- branches/tim.bunce/NYTProf.xs (original)
+++ branches/tim.bunce/NYTProf.xs Mon Jun 9 16:17:54 2008
@@ -109,8 +109,9 @@
void output_int(unsigned int);
void DB(pTHX);
void set_option(const char*);
-void open_output_file(char *);
-void init_runtime();
+void open_output_file(pTHX_ char *);
+void check_if_forked(pTHX);
+void set_options_from_env();
void init(pTHX);
void DEBUG_print_stats(pTHX);
HV *load_profile_data_from_stream();
@@ -604,23 +605,13 @@
#endif
}

- /* out should never be NULL, but perl sometimes falls into DB() AFTER
- it calls _finish() (which is ONLY used in END {...}. Strange!) */
if (!out)
return;

if (!firstrun) {
if (forkok) {
lock_file();
- if (last_pid != getpid()) { /* handle forks */
- if (trace_level >= 1)
- warn("New pid %d (was %d)\n", getpid(), last_pid);
- last_pid = getpid();
-#ifdef FPURGE
- FPURGE(out);
-#endif
- OUTPUT_PID();
- }
+ check_if_forked(aTHX);
}

fputc( (profile_blocks) ? '*' : '+', out);
@@ -715,26 +706,62 @@
* without the environment parsing overhead after each fork.
*/
void
-open_output_file(char *filename) {
+open_output_file(pTHX_ char *filename) {

+ char filename_buf[MAXPATHLEN];
const char *mode = "wb";
- int fd = -1;
- if (strEQ(filename, "STDOUT")) {
+ int fd = -2;
+
+ if (out && SvIV(PL_DBsingle)) { /* already opened so assume forking */
+ sprintf(filename_buf, "%s.%d.%d", filename, getpid(), getppid());
+ filename = filename_buf;
+ }
+ else if (strEQ(filename, "STDOUT")) {
fd = dup(STDOUT_FILENO);
- if (-1 == fd)
- perror("Unable to dup stdout");
- filename = NULL;
}
else if (strEQ(filename, "STDERR")) {
- fd = dup(STDOUT_FILENO);
- if (-1 == fd)
- perror("Unable to dup stdout");
- filename = NULL;
+ fd = dup(STDERR_FILENO);
+ }
+
+ if (-1 == fd) {
+ if (out) {
+ /* caller is expected to have purged old out if appropriate */
+ out = NULL;
+ }
+ }
+ else {
+ if (trace_level)
+ warn("Opening %s (%s)\n", filename, mode);
+
+ out = (filename) ? fopen(filename, mode) : fdopen(fd, mode);
+ }
+
+ if (!out) { /* failed to open */
+ /* disable profiling */
+ sv_setiv(PL_DBsingle, 0);
+ croak("Failed to open output '%s': %s", filename, strerror(errno));
}
- if (trace_level)
- warn("Opening %s (%s)\n", filename, mode);

- out = (filename) ? fopen(filename, mode) : fdopen(fd, mode);
+ print_header(aTHX);
+}
+
+
+void
+check_if_forked(pTHX) {
+ if (last_pid == getpid())
+ return;
+ if (trace_level >= 1)
+ warn("New pid %d (was %d)\n", getpid(), last_pid);
+ last_pid = getpid();
+#ifdef FPURGE
+ FPURGE(out);
+#endif
+ if (0) {
+ open_output_file(aTHX_ PROF_output_file);
+ }
+ else {
+ OUTPUT_PID();
+ }
}


@@ -816,7 +843,7 @@
* Populate runtime values from environment, the running script or use defaults
*/
void
-init_runtime() {
+set_options_from_env() {

/* Runtime configuration
Environment vars have lower priority */
@@ -868,14 +895,9 @@
hashtable.table = (Hash_entry**)safemalloc(hashtable_memwidth);
memset(hashtable.table, 0, hashtable_memwidth);

- init_runtime();
+ set_options_from_env();

- open_output_file(PROF_output_file);
- if (out == NULL) {
- croak("Failed to open output: %s", strerror(errno));
- }
-
- print_header(aTHX);
+ open_output_file(aTHX_ PROF_output_file);

intercept_opcodes(aTHX);

@@ -1456,7 +1478,7 @@
load_profile_data_from_file(file=NULL)
char *file;
CODE:
- init_runtime();
+ set_options_from_env();

if (strEQ(file,"STDIN")) {
int fd = dup(STDIN_FILENO);

Reply all
Reply to author
Forward
0 new messages