|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <string.h> |
| 4 | +#include <fcntl.h> |
| 5 | +#include <sys/types.h> |
| 6 | +#include <sys/stat.h> |
| 7 | +#include <sys/time.h> |
| 8 | +#include <time.h> |
| 9 | + |
| 10 | +// If I want a no mpi version, define this macro |
| 11 | +#ifdef NOMPI |
| 12 | +#define MPI_COMM_WORLD 0 |
| 13 | +int MPI_Initialized(int *result) { *result=1; } |
| 14 | +int MPI_Comm_rank(int unused, int *myrank ){ *myrank=0 ; } |
| 15 | +int MPI_Init(int *argc, char ***argv) { return 0; } |
| 16 | +int MPI_Finalize() { return 0 ;} |
| 17 | +#else |
| 18 | +#include <mpi.h> |
| 19 | +#endif |
| 20 | + |
| 21 | +static int fp=-1; |
| 22 | +static int mypid=-1; |
| 23 | +static int myrank=999; |
| 24 | + |
| 25 | +#ifdef DEBUG |
| 26 | +#define VERBOSE 1 |
| 27 | +#else |
| 28 | +#define VERBOSE 0 |
| 29 | +#endif |
| 30 | +#define USE_MPI 1 |
| 31 | +#define MAX_STR_LEN 2048 |
| 32 | +#define ROUGH_PREAMBLE_LENGTH 100 |
| 33 | + |
| 34 | +// |
| 35 | +// Returns unix time stamp with micro time appended |
| 36 | +long long f_gettimeofday_() { |
| 37 | + struct timeval tv; |
| 38 | + long long the_time; |
| 39 | + |
| 40 | + gettimeofday(&tv,NULL); |
| 41 | + the_time=tv.tv_sec; |
| 42 | + the_time=the_time*1000000 + tv.tv_usec; |
| 43 | + return (the_time); |
| 44 | +} |
| 45 | + |
| 46 | +/* Simple trim function taken from |
| 47 | + * http://stackoverflow.com/questions/122616/how-do-i-trim-leading-trailing-whitespace-in-a-standard-way |
| 48 | + * This function shifts the string into the first position of the buffer. |
| 49 | + * This way, if the string was dynamically allocated, it can be deallocated |
| 50 | + * on the original pointer. |
| 51 | + */ |
| 52 | +char *trim(char *str) { |
| 53 | + size_t len = 0; |
| 54 | + char *frontp = str - 1; |
| 55 | + char *endp = NULL; |
| 56 | + |
| 57 | + if( str == NULL ) return NULL; |
| 58 | + |
| 59 | + if( str[0] == '\0' ) return str; |
| 60 | + |
| 61 | + len = strlen(str); |
| 62 | + endp = str + len; |
| 63 | + |
| 64 | + /* Move the front and back pointers to address |
| 65 | + * the first non-whitespace characters from |
| 66 | + * each end. |
| 67 | + */ |
| 68 | + while( isspace(*(++frontp)) ); |
| 69 | + while( isspace(*(--endp)) && endp != frontp ); |
| 70 | + |
| 71 | + if ( str + len - 1 != endp ) |
| 72 | + *(endp + 1) = '\0'; |
| 73 | + else if( frontp != str && endp == frontp ) |
| 74 | + *str = '\0'; |
| 75 | + |
| 76 | + /* Shift the string so that it starts at str so |
| 77 | + * that if it's dynamically allocated, we can |
| 78 | + * still free it on the returned pointer. Note |
| 79 | + * the reuse of endp to mean the front of the |
| 80 | + * string buffer now. |
| 81 | + */ |
| 82 | + endp = str; |
| 83 | + if( frontp != str ) { |
| 84 | + while( *frontp ) *endp++ = *frontp++; |
| 85 | + *endp = '\0'; |
| 86 | + } |
| 87 | + |
| 88 | + return str; |
| 89 | +} |
| 90 | + |
| 91 | +// Function was first implemented under this name, |
| 92 | +// so now this is just a deprecated redirection function |
| 93 | +int mattdebug_(const char *str, int *lineno ,int str_len) { |
| 94 | + return fastdebug_(str, lineno, str_len); |
| 95 | +} |
| 96 | + |
| 97 | +int fastdebug_(const char *str, int *lineno ,int str_len) { |
| 98 | + |
| 99 | + int flag; |
| 100 | + mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; |
| 101 | + char *msg, *nstr; |
| 102 | + char hostname[30], fname[50]; |
| 103 | +// char prefix[] = "/tmp/gmdebug/debug-"; |
| 104 | + char *prefix = "/tmp/gmdebug/debug-"; |
| 105 | + char preamble[ROUGH_PREAMBLE_LENGTH]; |
| 106 | + |
| 107 | + MPI_Initialized(&flag); |
| 108 | + if(flag==0) return 1; |
| 109 | + if (fp == -1) { |
| 110 | + if(NULL!=getenv("MATT_PREFIX")) prefix=getenv("MATT_PREFIX"); |
| 111 | + mypid=getpid(); |
| 112 | + MPI_Initialized(&flag); |
| 113 | + if(flag) MPI_Comm_rank(MPI_COMM_WORLD, &myrank ); |
| 114 | + else fprintf(stderr,"[mattdebug] MPI NOT STARTED\n"); |
| 115 | + |
| 116 | + // just for debug.. |
| 117 | + if (VERBOSE) mypid=0; |
| 118 | + |
| 119 | + snprintf(fname,sizeof(fname)-1,"%s%5.5d-%3.3d",prefix,mypid,myrank); |
| 120 | + if (VERBOSE) printf("Opening file %s\n", fname); |
| 121 | + if ((fp=open(fname, O_WRONLY | O_CREAT | O_APPEND, mode)) < 0) { |
| 122 | + fprintf(stderr, "[mattdebug]: Cannot open file %s\n", fname); |
| 123 | + exit(1); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + // Get the hostname |
| 128 | + gethostname(hostname, sizeof hostname); |
| 129 | + |
| 130 | + // Write the preable, so, time, hostname, etc. |
| 131 | + snprintf(preamble, ROUGH_PREAMBLE_LENGTH+1, |
| 132 | + "%lld [%s:%05d:%02d]", |
| 133 | + f_gettimeofday_(), |
| 134 | + hostname, mypid, myrank); |
| 135 | + |
| 136 | + // Magic number ROUGH_PREAMBLE_LENGTH is estimated length of all the |
| 137 | + // host/pid/rank info in the debug line |
| 138 | + if ((str_len + strlen(preamble)) > MAX_STR_LEN) { |
| 139 | + // Truncate message |
| 140 | + str_len = MAX_STR_LEN - strlen(preamble); |
| 141 | + } |
| 142 | + str_len++; // null character |
| 143 | + |
| 144 | + // Trim white space |
| 145 | + if (VERBOSE) printf("[mattdebug] Trying to allocation %d characters for nstr \"%s\"\n", str_len, str); |
| 146 | + if ((nstr=(char *)alloca(str_len)) == NULL) { |
| 147 | + fprintf(stderr, "[mattdebug]: Cannot allocate msg %d characters for string.\n", (str_len+2)); |
| 148 | + exit(1); |
| 149 | + } |
| 150 | + snprintf(nstr, str_len, "%s", str); // copy it to a mutable stirng |
| 151 | + nstr=trim(nstr); // trim white space |
| 152 | + str_len=strlen(nstr); |
| 153 | + |
| 154 | + |
| 155 | + // str_len will now be the entire output message (preample and all) |
| 156 | + // The 128 is a magic number buffer, for the spaces and line breaks, etc.. |
| 157 | + str_len=str_len+strlen(preamble)+128; |
| 158 | + |
| 159 | + if (VERBOSE) printf("[mattdebug] Trying to allocation %d characters for combined string \"%s %s\"\n", str_len+2, preamble, nstr); |
| 160 | + if ((msg=(char *)alloca(str_len)) == NULL) { |
| 161 | + fprintf(stderr, "[mattdebug]: Cannot allocate msg %d characters for string.\n", (str_len+2)); |
| 162 | + exit(1); |
| 163 | + } |
| 164 | + |
| 165 | + // Combine the string |
| 166 | + snprintf(msg, str_len, "%s(%5d) %s\n", preamble,*lineno, nstr); |
| 167 | + //snprintf(msg, str_len+2, "%s %s\n", nstr); |
| 168 | + |
| 169 | + if (VERBOSE) printf("Writing \"%s\" to %s\n", msg, fname); |
| 170 | + write(fp,msg,strlen(msg)); |
| 171 | + |
| 172 | + /*close(fp);*/ |
| 173 | + |
| 174 | + return 1; |
| 175 | +} |
| 176 | + |
| 177 | + |
| 178 | +// This is done to allow me to either directly build this |
| 179 | +// as an executable, or build it with s.compile using a bidon |
| 180 | +#if defined(TEST) |
| 181 | +int main(int argc, char **argv) { |
| 182 | +#else |
| 183 | +int mattmain_(int argc, char **argv) { |
| 184 | +#endif |
| 185 | + char msg[] = "Hello"; |
| 186 | + long long the_time; |
| 187 | + int lineno; |
| 188 | + |
| 189 | + MPI_Init(&argc, &argv); |
| 190 | + |
| 191 | + the_time=f_gettimeofday_(); |
| 192 | + printf("time=%lld\n", the_time); |
| 193 | + |
| 194 | + lineno = 1234; |
| 195 | + fastdebug_(msg,&lineno, strlen(msg)); |
| 196 | + lineno = 2345; |
| 197 | + fastdebug_("Hello again",&lineno, 11); |
| 198 | + lineno = 3456; |
| 199 | + fastdebug_("Testing trim ",&lineno, 23); |
| 200 | + |
| 201 | + MPI_Finalize(); |
| 202 | + |
| 203 | + return 0; |
| 204 | +} |
| 205 | + |
0 commit comments