void initializeHostInfoDataFromFileEntry(apr_pool_t* tmp_pool, struct hostinfo_holder_t* holder, char* primary_hostname, char* hostEntry, int hostType, char* smon_bin_dir, char* smon_log_dir) { holder->hostname = apr_pstrdup(tmp_pool, primary_hostname); CHECKMEM(holder->hostname); if (smon_bin_dir && smon_log_dir) { holder->smon_dir = apr_pstrdup(tmp_pool, smon_bin_dir); CHECKMEM(holder->smon_dir); holder->datadir = apr_pstrdup(tmp_pool, smon_log_dir); CHECKMEM(holder->datadir); } else { holder->smon_dir = NULL; holder->datadir = apr_pstrdup(tmp_pool, "/opt/dca/var/gpsmon"); CHECKMEM(holder->datadir); } switch(hostType) { case GPMON_HOSTTTYPE_HDW: holder->is_hdw = 1; break; case GPMON_HOSTTTYPE_HDM: holder->is_hdm = 1; break; case GPMON_HOSTTTYPE_ETL: holder->is_etl = 1; break; case GPMON_HOSTTTYPE_HBW: holder->is_hbw = 1; break; case GPMON_HOSTTTYPE_HDC: holder->is_hdc = 1; break; } holder->address_count = 0; int firstAddress = 1; while(*hostEntry) { char* location = strchr(hostEntry, ','); if (location) *location = 0; initializeHostInfoDataWithAddress(holder, hostEntry, firstAddress); holder->address_count++; if (!location) return; // there were no commas so this is the last address in the hostEntry *location = ','; hostEntry = location+1; firstAddress = 0; } }
/* filssalloc */ filamentssptr filssalloc(filamentssptr filss,int maxfil) { int f,newfilss; char **newnames; filamentptr *newfillist; if(maxfil<1) return NULL; newfilss=0; newnames=NULL; newfillist=NULL; if(!filss) { // new allocation filss=(filamentssptr) malloc(sizeof(struct filamentsuperstruct)); CHECKMEM(filss); newfilss=1; filss->condition=SCinit; filss->sim=NULL; filss->maxfil=0; filss->nfil=0; filss->fnames=NULL; filss->fillist=NULL; } else { // minor check if(maxfil<filss->maxfil) return NULL; } if(maxfil>filss->maxfil) { // allocate new fil names and fils CHECKMEM(newnames=(char**) calloc(maxfil,sizeof(char*))); for(f=0;f<maxfil;f++) newnames[f]=NULL; for(f=0;f<filss->maxfil;f++) newnames[f]=filss->fnames[f]; for(;f<maxfil;f++) CHECKMEM(newnames[f]=EmptyString()); CHECKMEM(newfillist=(filamentptr*) calloc(maxfil,sizeof(filamentptr))); // fil list for(f=0;f<maxfil;f++) newfillist[f]=NULL; for(f=0;f<filss->maxfil;f++) newfillist[f]=filss->fillist[f]; for(;f<maxfil;f++) { CHECKMEM(newfillist[f]=filalloc(100)); //?? fixed number of monomers for now at 100 newfillist[f]->filss=filss; newfillist[f]->fname=newnames[f]; }} filss->maxfil=maxfil; free(filss->fnames); filss->fnames=newnames; free(filss->fillist); filss->fillist=newfillist; return filss; failure: filssfree(filss); simLog(NULL,10,"Unable to allocate memory in filssalloc"); return NULL; }
void gpdb_get_single_string_from_query(const char* QUERY, char** resultstring, apr_pool_t* pool) { PGconn* conn = 0; PGresult* result = 0; char* tmpoutput = 0; int rowcount; const char* errmsg = gpdb_exec(&conn, &result, QUERY); if (errmsg) { gpmon_warning(FLINE, "GPDB error %s\n\tquery: %s\n", errmsg, QUERY); } else { rowcount = PQntuples(result); if (rowcount == 1) { tmpoutput = PQgetvalue(result, 0, 0); } else if (rowcount > 1) { gpmon_warning(FLINE, "unexpected number of rows returned from query %s", QUERY); } if (tmpoutput) { tmpoutput = apr_pstrdup(pool, gpmon_trim(tmpoutput)); CHECKMEM(tmpoutput); } } PQclear(result); PQfinish(conn); *resultstring = tmpoutput; }
/** * This local helper function allocates a gp_smon_to_mmon_packet_t and copies the gpmon_packet_t to it * @note This function should never be called with a qexec packet! */ static gp_smon_to_mmon_packet_t* gx_pkt_to_smon_to_mmon(apr_pool_t* pool, const gpmon_packet_t* pkt) { gp_smon_to_mmon_packet_t* t = apr_palloc(pool, sizeof(*t)); CHECKMEM(t); gp_smon_to_mmon_set_header(t, pkt->pkttype); copy_union_packet_gp_smon_to_mmon(t, pkt); return t; }
void gpdb_get_master_data_dir(char** hostname, char** mstrdir, apr_pool_t* pool) { PGconn* conn = 0; PGresult* result = 0; const char* QUERY = "select * from master_data_dir"; char* dir = 0; char* hname = 0; int rowcount; const char* errmsg = gpdb_exec(&conn, &result, QUERY); if (errmsg) { gpmon_warning(FLINE, "GPDB error %s\n\tquery: %s\n", errmsg, QUERY); } else { rowcount = PQntuples(result); if (rowcount > 0) { hname = PQgetvalue(result, 0, 0); dir = PQgetvalue(result, 0, 1); } if (!hname || !dir) { gpmon_warning(FLINE, "unable to get master data directory"); } else { hname = apr_pstrdup(pool, gpmon_trim(hname)); CHECKMEM(hname); dir = apr_pstrdup(pool, gpmon_trim(dir)); CHECKMEM(dir); } } PQclear(result); PQfinish(conn); *hostname = hname; *mstrdir = dir; }
void initializeHostInfoDataWithAddress(struct hostinfo_holder_t* holder, char* address, int firstAddress) { // USE permenant memory to store this data addressinfo_holder_t* aiholder = calloc(1, sizeof(addressinfo_holder_t)); CHECKMEM(aiholder); aiholder->address = strdup(address); CHECKMEM(aiholder->address); if (firstAddress) { holder->addressinfo_head = holder->addressinfo_tail = aiholder; } else { holder->addressinfo_tail->next = aiholder; holder->addressinfo_tail = aiholder; } }
/* Todo: Optimize this later*/ void intersect_friends(char * str1, char * str2) { //char temp[MAX_LINE_LEN]; char * temp = ( char * )calloc(40*MAX_LINE_LEN, sizeof(char)); char friend1[MAX_LINE_LEN]; char friend2[MAX_LINE_LEN]; CHECKMEM(temp); //char * str1 = &str1_b[0]; //char * str2 = &str2_b[0]; int n1; int n2; int idx1 = 0; int idx2 = 0; while(sscanf(&str1[idx1] ,"%s %n", friend1, &n1) > 0) { idx1 += n1; idx2 = 0; if(idx1 > MAX_LINE_LEN) { FPRINTF(stderr, "\tOOB: idx1: %d\n",idx1); free(temp); return; } while(sscanf(&str2[idx2], "%s %n", friend2, &n2) > 0) { idx2 += n2; // FPRINTF(stderr, "\tF1: %s\n",friend1); if(idx2 > MAX_LINE_LEN) { FPRINTF(stderr, "\tOOB: idx2: %d\n",idx2); free(temp); return; } if(strcmp(friend1, friend2) == 0) { strcat(temp, friend2); strcat(temp, " "); FPRINTF(stderr, "\tF2: idx1 %d idx2 %d %s\n",idx1, idx2, friend2); } } } //puts(temp); //Super Dangerous FPRINTF(stderr, "\tT: %s\n",temp); strcpy(str1, temp); free(temp); //memcpy(str1,temp, MAX_LINE_LEN); }
static void extract_segments_exec(gpmon_packet_t* pkt) { gpmon_qexec_t *p; gp_smon_to_mmon_packet_t *rec; gpmon_query_seginfo_key_t qseg_key; pidrec_t *pidrec; if (pkt->pkttype != GPMON_PKTTYPE_QEXEC) gpsmon_fatal(FLINE, "assert failed; expected pkttype qexec"); p = &pkt->u.qexec; qseg_key.qkey.tmid = p->key.tmid; qseg_key.qkey.ssid = p->key.ssid; qseg_key.qkey.ccnt = p->key.ccnt; qseg_key.segid = p->key.hash_key.segid; rec = apr_hash_get(gx.querysegtab, &qseg_key, sizeof(qseg_key)); pidrec = apr_hash_get(gx.pidtab, &p->key.hash_key.pid, sizeof(p->key.hash_key.pid)); ASSERT(pidrec); if (rec) { rec->u.queryseg.sum_cpu_elapsed += pidrec->cpu_elapsed; rec->u.queryseg.sum_measures_rows_in += p->rowsin; if (p->key.hash_key.segid == -1 && p->key.hash_key.nid == 1) { rec->u.queryseg.final_rowsout = p->rowsout; } } else { rec = apr_palloc(apr_hash_pool_get(gx.querysegtab), sizeof(gp_smon_to_mmon_packet_t)); CHECKMEM(rec); gp_smon_to_mmon_set_header(rec, GPMON_PKTTYPE_QUERYSEG); rec->u.queryseg.key = qseg_key; if (p->key.hash_key.segid == -1 && p->key.hash_key.nid == 1) { rec->u.queryseg.final_rowsout = p->rowsout; } else { rec->u.queryseg.final_rowsout = -1; } rec->u.queryseg.sum_cpu_elapsed = pidrec->cpu_elapsed; rec->u.queryseg.sum_measures_rows_in = p->rowsin; apr_hash_set(gx.querysegtab, &rec->u.queryseg.key, sizeof(rec->u.queryseg.key), rec); } }
JobQueue *JobQueue_new(int maxThreads, void *threadData, void *(*ThreadState_new) (void *), void (*ThreadState_free) (void *)) { int i; JobQueue *jq = malloc(sizeof(JobQueue)); CHECKMEM(jq); jq->todo = NULL; jq->acceptingJobs = true; jq->idle = jq->nThreads = 0; jq->maxThreads = maxThreads; jq->threadData = threadData; jq->ThreadState_new = ThreadState_new; jq->ThreadState_free = ThreadState_free; // set attr for detached threads if((i = pthread_attr_init(&jq->attr))) { fprintf(stderr, "%s:%d: pthread_attr_init returned %d (%s)", __FILE__, __LINE__, i, strerror(i)); exit(1); } if((i = pthread_attr_setdetachstate(&jq->attr, PTHREAD_CREATE_DETACHED))) { fprintf(stderr, "%s:%d: pthread_attr_setdetachstate returned %d (%s)", __FILE__, __LINE__, i, strerror(i)); exit(1); } if((i = pthread_mutex_init(&jq->lock, NULL))) { fprintf(stderr, "%s:%d: pthread_mutex_init returned %d (%s)", __FILE__, __LINE__, i, strerror(i)); exit(1); } if((i = pthread_cond_init(&jq->wakeWorker, NULL))) { fprintf(stderr, "%s:%d: pthread_cond_init returned %d (%s)", __FILE__, __LINE__, i, strerror(i)); exit(1); } if((i = pthread_cond_init(&jq->wakeMain, NULL))) { fprintf(stderr, "%s:%d: pthread_cond_init returned %d (%s)", __FILE__, __LINE__, i, strerror(i)); exit(1); } jq->valid = JOBQUEUE_VALID; return jq; }
static void setup_gx(int port, apr_int64_t signature) { int e; apr_pool_t* subpool; /* set up pool */ if (0 != (e = apr_pool_create_alloc(&gx.pool, 0))) { gpsmon_fatalx(FLINE, e, "apr_pool_create_alloc failed"); } /* set up port, event */ gx.port = port; gx.signature = signature; if (!event_init()) { gpsmon_fatalx(FLINE, APR_FROM_OS_ERROR(errno), "event_init failed"); } if (0 != (e = apr_pool_create_alloc(&subpool, gx.pool))) { gpsmon_fatalx(FLINE, e, "apr_pool_create_alloc failed"); } /* qexec hash table */ gx.qexectab = apr_hash_make(subpool); CHECKMEM(gx.qexectab); /* qlog hash table */ gx.qlogtab = apr_hash_make(subpool); CHECKMEM(gx.qlogtab); /* segment hash table */ gx.segmenttab = apr_hash_make(subpool); CHECKMEM(gx.segmenttab); /* queryseg hash table */ gx.querysegtab = apr_hash_make(subpool); CHECKMEM(gx.querysegtab); /* pidtab */ gx.pidtab = apr_hash_make(subpool); CHECKMEM(gx.pidtab); /* device metrics hashes */ net_devices = apr_hash_make(gx.pool); CHECKMEM(net_devices); disk_devices = apr_hash_make(gx.pool); CHECKMEM(disk_devices); }
/** * Waits until there is a job in the queue, pops it off and executes * it, then waits for another. Runs until jobs are completed and * main thread sets acceptingJobs=0. */ void *threadfun(void *arg) { DPRINTF(("%s %lu entry\n", __func__, (unsigned long) pthread_self())); // struct timespec timeout; JobQueue *jq = (JobQueue *) arg; Job *job; int status; void *threadState = NULL; if(jq->ThreadState_new != NULL) { threadState = jq->ThreadState_new(jq->threadData); CHECKMEM(threadState); } for(;;) { // clock_gettime(CLOCK_REALTIME, &timeout); // timeout.tv_sec += 3; status = pthread_mutex_lock(&jq->lock); // LOCK if(status) ERR(status, "lock"); else DPRINTF(("%s:%s:%d: locked\n", __FILE__, __func__, __LINE__)); // Wait while the queue is empty and accepting jobs while(NULL == jq->todo && jq->acceptingJobs) { DPRINTF(("%s:%d: awaiting work. todo=%p\n", __func__, __LINE__, jq->todo)); ++jq->idle; if(jq->idle == jq->nThreads) { status = pthread_cond_signal(&jq->wakeMain); if(status) ERR(status, "signal wakeMain"); } //status = pthread_cond_timedwait(&jq->wakeWorker, &jq->lock, // &timeout); status = pthread_cond_wait(&jq->wakeWorker, &jq->lock); --jq->idle; //if(status == ETIMEDOUT) // continue; if(status) ERR(status, "wait wakeWorker"); } /* * todo accepting * 0 0 <- exit * 0 1 <- stay in while loop * 1 0 <- do work * 1 1 <- do work */ if(NULL == jq->todo) { // shutting down assert(!jq->acceptingJobs); break; } else { // do job DPRINTF(("%s %lu got work\n", __func__, (unsigned long) pthread_self())); // remove job from queue assert(NULL != jq->todo); job = jq->todo; jq->todo = jq->todo->next; #ifdef DPRINTF_ON printf("%s:%d:queue:", __func__, __LINE__); Job_print(jq->todo); #endif status = pthread_mutex_unlock(&jq->lock); // UNLOCK if(status) ERR(status, "unlock"); else DPRINTF(("%s:%s:%d: unlocked\n", __FILE__, __func__, __LINE__)); DPRINTF(("%s %lu calling jobfun\n", __func__, (unsigned long) pthread_self())); job->jobfun(job->param, threadState); DPRINTF(("%s %lu back fr jobfun\n", __func__, (unsigned long) pthread_self())); free(job); } } // still have lock --jq->nThreads; status = pthread_cond_signal(&jq->wakeMain); if(status) ERR(status, "signal wakeMain"); status = pthread_mutex_unlock(&jq->lock); // UNLOCK if(status) ERR(status, "unlock"); else DPRINTF(("%s:%s:%d: unlocked\n", __FILE__, __func__, __LINE__)); if(threadState) jq->ThreadState_free(threadState); DPRINTF(("%s %lu exit\n", __func__, (unsigned long) pthread_self())); return NULL; }
static void setup_sigar(void) { sigar_file_system_list_t sigar_fslist; sigar_net_interface_list_t sigar_netlist; int i, e, cnt; int do_destroy = 0; /* initialize sigar */ if (0 != (e = sigar_open(&gx.sigar))) { gpsmon_fatalx(FLINE, e, "sigar_open failed"); } TR2(("sigar initialized\n")); do_destroy = 1; if (0 != sigar_net_interface_list_get(gx.sigar, &sigar_netlist)) { memset(&sigar_netlist, 0, sizeof(sigar_netlist)); do_destroy = 0; } gx.netlist = apr_pcalloc(gx.pool, sizeof(const char*) * (1 + sigar_netlist.number)); CHECKMEM(gx.netlist); for (i = 0; i < sigar_netlist.number; i++) { gx.netlist[i] = apr_pstrdup(gx.pool, sigar_netlist.data[i]); CHECKMEM(gx.netlist[i]); TR2(("sigar net %d: %s\n", i, gx.netlist[i])); } if (do_destroy) sigar_net_interface_list_destroy(gx.sigar, &sigar_netlist); do_destroy = 1; if (0 != sigar_file_system_list_get(gx.sigar, &sigar_fslist)) { memset(&sigar_fslist, 0, sizeof(sigar_fslist)); do_destroy = 0; } cnt = 0; TR2(("sigar fsnumber: %d\n", sigar_fslist.number)); for (i = 0; i < sigar_fslist.number; i++) { if (sigar_fslist.data[i].type == SIGAR_FSTYPE_LOCAL_DISK) { TR2(("sigar cnt: %d\n", cnt + 1)); cnt++; } } gx.fslist = apr_pcalloc(gx.pool, sizeof(const char*) * (cnt + 1)); CHECKMEM(gx.fslist); gx.devlist = apr_pcalloc(gx.pool, sizeof(const char*) * (cnt + 1)); CHECKMEM(gx.devlist); cnt = 0; for (i = 0; i < sigar_fslist.number; i++) { if (sigar_fslist.data[i].type == SIGAR_FSTYPE_LOCAL_DISK) { gx.fslist[cnt] = apr_pstrdup(gx.pool, sigar_fslist.data[i].dir_name); CHECKMEM(gx.fslist[cnt]); TR2(("fs: %s\n", gx.fslist[cnt])); gx.devlist[cnt] = apr_pstrdup(gx.pool, sigar_fslist.data[i].dev_name); CHECKMEM(gx.devlist[cnt]); cnt++; } } cnt = 0; for (i = 0; i < sigar_fslist.number; i++) { if (sigar_fslist.data[i].type == SIGAR_FSTYPE_LOCAL_DISK || sigar_fslist.data[i].type == SIGAR_FSTYPE_NETWORK) { TR2(("sigar cnt: %d\n", cnt + 1)); cnt++; } } gx.allfslist = apr_pcalloc(gx.pool, sizeof(const char*) * (cnt + 1)); CHECKMEM(gx.allfslist); cnt = 0; for (i = 0; i < sigar_fslist.number; i++) { if (sigar_fslist.data[i].type == SIGAR_FSTYPE_LOCAL_DISK || sigar_fslist.data[i].type == SIGAR_FSTYPE_NETWORK) { gx.allfslist[cnt] = apr_pstrdup(gx.pool, sigar_fslist.data[i].dir_name); CHECKMEM(gx.allfslist[cnt]); TR2(("allfs: %s\n", gx.allfslist[cnt])); cnt++; } } if (do_destroy) sigar_file_system_list_destroy(gx.sigar, &sigar_fslist); }
//http://www.daniweb.com/software-development/c/threads/206198/intersection-of-two-strings void myReduceLines( void ) { /* char line[MAX_LINE_LEN]; char key[MAX_KEY_LEN]; char prev_key[MAX_KEY_LEN]; char friends[MAX_LINE_LEN]; char mutual_friends[MAX_LINE_LEN]; char outputs[MAX_LINE_LEN]; */ // Running out of static memory, need to allocate more in some cases // I should do this with a list char *line = (char *) calloc(MAX_LINE_LEN, sizeof(char)); char *key = (char *) calloc(MAX_KEY_LEN, sizeof(char) ); char *prev_key = (char *) calloc(MAX_KEY_LEN, sizeof(char) ); char *friends = (char *) calloc(40*MAX_LINE_LEN, sizeof(char)); char *mutual_friends = (char *) calloc(40*MAX_LINE_LEN, sizeof(char)); char *outputs = (char *) calloc(40*MAX_LINE_LEN, sizeof(char)); //Check whether we actually allocated the memory CHECKMEM(line); CHECKMEM(key ); CHECKMEM(prev_key ); CHECKMEM(friends ); CHECKMEM(mutual_friends); CHECKMEM(outputs ); char *result; int key_count = 0; int idx; while((result = fgets(line, MAX_LINE_LEN, stdin)) != NULL) { if(ferror(stdin)) { perror("Error reading stdin."); } while(line[idx]) { if(line[idx] == '\n') { line[idx] = ' '; } idx++; } idx = 0; //if( sscanf(line , "%[^\t]s\t%[^\t\n\0]s", key, friends) > 0) //Get the key first // Note should probably break this up into multiple sub-keys if( sscanf(line , "%[^\t]%n", key, &idx) > 0) { strcpy(friends, &line[idx]); //fputs(key, stderr); FPRINTF(stderr, "\tK: %s\n", key); FPRINTF(stderr, "\tN: %d\n", idx); FPRINTF(stderr, "\tFA: %s\n", friends); //fputs(friends, stderr); if(strcmp(prev_key,key) == 0) //If Keys are equal { key_count += 1; //Intersect the friends lists intersect_friends(mutual_friends, friends); }else //Keys are not the same { //Output the mutual friend list and move on to the next set if(key_count != 0) { strcpy(outputs, prev_key); strcat(outputs, "\t"); strcat(outputs, mutual_friends); //printf("%s\t%d\n",prev_key,key_count); puts(outputs); } strcpy(prev_key,key); strcpy(mutual_friends,friends); key_count = 0; }// end else words not equivalent } } //puts(outputs); if(ferror(stdin)) { perror("Error reading stdin."); } // Free buffers free(outputs ); free(mutual_friends); free(friends ); free(prev_key ); free(key ); free(line ); }
char * formataddr (char *orig, char *str) { register int len; register int isgroup; register char *dst; register char *cp; register char *sp; register struct mailname *mp = NULL; /* if we don't have a buffer yet, get one */ if (bufsiz == 0) { buf = mh_xmalloc (BUFINCR); last_dst = buf; /* XXX */ bufsiz = BUFINCR - 6; /* leave some slop */ bufend = buf + bufsiz; } /* * If "orig" points to our buffer we can just pick up where we * left off. Otherwise we have to copy orig into our buffer. */ if (orig == buf) dst = last_dst; else if (!orig || !*orig) { dst = buf; *dst = '\0'; } else { dst = last_dst; /* XXX */ CHECKMEM (orig); CPY (orig); } /* concatenate all the new addresses onto 'buf' */ for (isgroup = 0; (cp = getname (str)); ) { if ((mp = getm (cp, NULL, 0, fmt_norm, NULL)) == NULL) continue; if (isgroup && (mp->m_gname || !mp->m_ingrp)) { *dst++ = ';'; isgroup = 0; } /* if we get here we're going to add an address */ if (dst != buf) { *dst++ = ','; *dst++ = ' '; } if (mp->m_gname) { CHECKMEM (mp->m_gname); CPY (mp->m_gname); isgroup++; } sp = adrformat (mp); CHECKMEM (sp); CPY (sp); mnfree (mp); } if (isgroup) *dst++ = ';'; *dst = '\0'; last_dst = dst; return (buf); }
void process_line_in_devices_cnf(apr_pool_t* tmp_pool, apr_hash_t* htab, char* line) { if (!line) { gpmon_warningx(FLINE, 0, "Line in devices file is null, skipping"); return; } char* host; char* device; char* category; char primary_hostname[64]; char* location = strchr(line, '#'); if (location) { *location = 0; // remove comments from the line } if (!line) { gpmon_warningx(FLINE, 0, "Line in devices file is null after removing comments, skipping"); return; } // we do these in reverse order so inserting null chars does not prevent finding other tokens if (find_token_in_config_string(line, &host, "Host")) { return; } if (find_token_in_config_string(line, &device, "Device")) { return; } if (find_token_in_config_string(line, &category, "Category")) { return; } int monitored_device = 0; int hostType = 0; if (strcmp(device, "Spidey0001") == 0) { monitored_device = 1; hostType = GPMON_HOSTTTYPE_HDW; } if (strcmp(device, "Spidey0002") == 0) { monitored_device = 1; hostType = GPMON_HOSTTTYPE_HDM; } if (strcmp(device, "Spidey0003") == 0) { monitored_device = 1; hostType = GPMON_HOSTTTYPE_HBW; } if (strcmp(device, "EtlHost") == 0) { monitored_device = 1; hostType = GPMON_HOSTTTYPE_ETL; } //For V2 if (strcmp(device, "Locust0001") == 0) { monitored_device = 1; hostType = GPMON_HOSTTTYPE_HDW; } if (strcmp(device, "Locust0002") == 0) { monitored_device = 1; hostType = GPMON_HOSTTTYPE_HDM; } if (strcmp(device, "Locust0003") == 0) { monitored_device = 1; hostType = GPMON_HOSTTTYPE_HDC; } if (strcmp(device, "EtlHostV2") == 0) { monitored_device = 1; hostType = GPMON_HOSTTTYPE_ETL; } // segment host, switch, etc ... we are only adding additional hosts required for performance monitoring if (!monitored_device) return; strncpy(primary_hostname, host, sizeof(primary_hostname)); primary_hostname[sizeof(primary_hostname) - 1] = 0; location = strchr(primary_hostname, ','); if (location) *location = 0; struct hostinfo_holder_t* hostinfo_holder = apr_hash_get(htab, primary_hostname, APR_HASH_KEY_STRING); if (hostinfo_holder) { gpmon_warningx(FLINE, 0, "Host '%s' is duplicated in devices.cnf", primary_hostname); return; } // OK Lets add this record at this point hostinfo_holder = apr_pcalloc(tmp_pool, sizeof(struct hostinfo_holder_t)); CHECKMEM(hostinfo_holder); apr_hash_set(htab, primary_hostname, APR_HASH_KEY_STRING, hostinfo_holder); initializeHostInfoDataFromFileEntry(tmp_pool, hostinfo_holder, primary_hostname, host, hostType, NULL, NULL); }
void process_line_in_hadoop_cluster_info(apr_pool_t* tmp_pool, apr_hash_t* htab, char* line, char* smon_bin_location, char* smon_log_location) { if (!line) { gpmon_warningx(FLINE, 0, "Line in hadoop cluster info file is null, skipping"); return; } char* host; char* category; char primary_hostname[64]; char* location = strchr(line, '#'); if (location) { *location = 0; // remove comments from the line } if (!line) { gpmon_warningx(FLINE, 0, "Line in devices file is null after removing comments, skipping"); return; } // we do these in reverse order so inserting null chars does not prevent finding other tokens if (find_token_in_config_string(line, &category, "Categories")) { return; } location = strchr(category, ','); //remove the comma and extra categories if (location) { *location = 0; } if (find_token_in_config_string(line, &host, "Hostname")) { return; } TR1(("Found hadoop host %s\n",host )); // look for the 3 hadoop host types int monitored_device = 0; int hostType = 0; if (strcmp(category, "hdm") == 0) { monitored_device = 1; hostType = GPMON_HOSTTTYPE_HDM; } if (strcmp(category, "hdw") == 0) { monitored_device = 1; hostType = GPMON_HOSTTTYPE_HDW; } if (strcmp(category, "hdc") == 0) { monitored_device = 1; hostType = GPMON_HOSTTTYPE_HDC; } // The below code is the same as the devices file parsing code // segment host, switch, etc ... we are only adding additional hosts required for performance monitoring if (!monitored_device) { return; } strncpy(primary_hostname, host, sizeof(primary_hostname)); primary_hostname[sizeof(primary_hostname) - 1] = 0; location = strchr(primary_hostname, ','); if (location) { *location = 0; } struct hostinfo_holder_t* hostinfo_holder = apr_hash_get(htab, primary_hostname, APR_HASH_KEY_STRING); if (hostinfo_holder) { gpmon_warningx(FLINE, 0, "Host '%s' is duplicated in clusterinfo.txt", primary_hostname); return; } // OK Lets add this record at this point hostinfo_holder = apr_pcalloc(tmp_pool, sizeof(struct hostinfo_holder_t)); CHECKMEM(hostinfo_holder); apr_hash_set(htab, primary_hostname, APR_HASH_KEY_STRING, hostinfo_holder); initializeHostInfoDataFromFileEntry(tmp_pool, hostinfo_holder, primary_hostname, host, hostType, smon_bin_location, smon_log_location); }
static void get_pid_metrics(apr_int32_t pid, apr_int32_t tmid, apr_int32_t ssid, apr_int32_t ccnt) { apr_int32_t status; sigar_proc_cpu_t cpu; sigar_proc_mem_t mem; sigar_proc_fd_t fd; pidrec_t* rec; apr_pool_t* pool = apr_hash_pool_get(gx.pidtab); rec = apr_hash_get(gx.pidtab, &pid, sizeof(pid)); if (rec && rec->updated_tick == gx.tick) return; /* updated in current cycle */ memset(&cpu, 0, sizeof(cpu)); memset(&mem, 0, sizeof(mem)); memset(&fd, 0, sizeof(fd)); TR2(("--------------------- starting %d\n", pid)); if (!rec) { sigar_proc_exe_t exe; /* There might be cases where the pid no longer exist, so we'll just * zero out the memory first before doing anything */ rec = apr_pcalloc(pool, sizeof(*rec)); CHECKMEM(rec); rec->pid = pid; rec->query_key.tmid = tmid; rec->query_key.ssid = ssid; rec->query_key.ccnt = ccnt; rec->pname = rec->cwd = 0; if (0 == sigar_proc_exe_get(gx.sigar, pid, &exe)) { rec->pname = apr_pstrdup(pool, exe.name); rec->cwd = apr_pstrdup(pool, exe.root); } if (!rec->pname) rec->pname = "unknown"; if (!rec->cwd) rec->cwd = "unknown"; apr_hash_set(gx.pidtab, &rec->pid, sizeof(rec->pid), rec); } status = sigar_proc_mem_get(gx.sigar, pid, &mem); /* ESRCH is error 3: (No such process) */ if (status != SIGAR_OK) { if (status != ESRCH) { TR2(("[WARNING] %s. PID: %d\n", sigar_strerror(gx.sigar, status), pid)); } return; } status = sigar_proc_cpu_get(gx.sigar, pid, &cpu); if (status != SIGAR_OK) { if (status != ESRCH) { TR2(("[WARNING] %s. PID: %d\n", sigar_strerror(gx.sigar, status), pid)); } return; } status = sigar_proc_fd_get(gx.sigar, pid, &fd); if (status != SIGAR_OK) { if (status != ESRCH) { TR2(("[WARNING] %s. PID: %d\n", sigar_strerror(gx.sigar, status), pid)); } return; } rec->updated_tick = gx.tick; rec->p_metrics.fd_cnt = (apr_uint32_t) fd.total; rec->p_metrics.cpu_pct = (float) (cpu.percent * cpu_cores_utilization_multiplier); rec->p_metrics.mem.size = mem.size; rec->p_metrics.mem.resident = mem.resident; #ifdef __linux__ rec->p_metrics.mem.share = mem.share; #else rec->p_metrics.mem.share = 0; #endif rec->cpu_elapsed = cpu.total; }
void JobQueue_addJob(JobQueue * jq, int (*jobfun) (void *, void *), void *param) { assert(jq); int status; pthread_t id; if(jq->valid != JOBQUEUE_VALID) { fprintf(stderr, "%s:%d: JobQueue not initialized", __func__, __LINE__); exit(1); } if(!jq->acceptingJobs) { fprintf(stderr, "%s:%s:%d: JobQueue not accepting jobs\n", __FILE__, __func__, __LINE__); exit(1); } Job *job = malloc(sizeof(Job)); CHECKMEM(job); job->jobfun = jobfun; job->param = param; status = pthread_mutex_lock(&jq->lock); if(status) ERR(status, "lock"); job->next = jq->todo; jq->todo = job; #ifdef DPRINTF_ON printf("%s:%d:queue:", __func__, __LINE__); Job_print(jq->todo); #endif // If threads are idling, wake one if(jq->idle > 0) { status = pthread_cond_signal(&jq->wakeWorker); if(status) ERR(status, "signal wakeWorker"); } else if(jq->nThreads < jq->maxThreads) { // launch a new thread DPRINTF(("%s:%d launching thread\n", __func__, __LINE__)); status = pthread_create(&id, &jq->attr, threadfun, (void *) jq); if(status) { fprintf(stderr, "%s:%d: pthread_create returned %d (%s)\n", __func__, __LINE__, status, strerror(status)); exit(1); } ++jq->nThreads; } status = pthread_mutex_unlock(&jq->lock); if(status) ERR(status, "unlock"); else DPRINTF(("%s:%s:%d: unlocked\n", __FILE__, __func__, __LINE__)); }
void gpdb_get_hostlist(int* hostcnt, host_t** host_table, apr_pool_t* global_pool, mmon_options_t* opt) { apr_pool_t* pool; PGconn* conn = 0; PGresult* result = 0; int rowcount, i; unsigned int unique_hosts = 0; apr_hash_t* htab; struct hostinfo_holder_t* hostinfo_holder = NULL; host_t* hosts = NULL; int e; // 0 -- hostname, 1 -- address, 2 -- datadir, 3 -- is_master, const char *QUERY = "SELECT distinct hostname, address, case when content < 0 then 1 else 0 end as is_master, MAX(fselocation) as datadir FROM pg_filespace_entry " "JOIN gp_segment_configuration on (dbid = fsedbid) WHERE fsefsoid = (select oid from pg_filespace where fsname='pg_system') " "GROUP BY (hostname, address, is_master) order by hostname"; if (0 != (e = apr_pool_create_alloc(&pool, NULL))) { gpmon_fatalx(FLINE, e, "apr_pool_create_alloc failed"); } const char* errmsg = gpdb_exec(&conn, &result, QUERY); TR2((QUERY)); TR2(("\n")); if (errmsg) { gpmon_warning(FLINE, "GPDB error %s\n\tquery: %s\n", errmsg, QUERY); } else { // hash of hostnames to addresses htab = apr_hash_make(pool); rowcount = PQntuples(result); for (i = 0; i < rowcount; i++) { char* curr_hostname = PQgetvalue(result, i, 0); hostinfo_holder = apr_hash_get(htab, curr_hostname, APR_HASH_KEY_STRING); if (!hostinfo_holder) { hostinfo_holder = apr_pcalloc(pool, sizeof(struct hostinfo_holder_t)); CHECKMEM(hostinfo_holder); apr_hash_set(htab, curr_hostname, APR_HASH_KEY_STRING, hostinfo_holder); hostinfo_holder->hostname = curr_hostname; hostinfo_holder->is_master = atoi(PQgetvalue(result, i, 2)); hostinfo_holder->datadir = PQgetvalue(result, i, 3); // use permenant memory for address list -- stored for duration // populate 1st on list and save to head and tail hostinfo_holder->addressinfo_head = hostinfo_holder->addressinfo_tail = calloc(1, sizeof(addressinfo_holder_t)); CHECKMEM(hostinfo_holder->addressinfo_tail); // first is the hostname hostinfo_holder->addressinfo_tail->address = strdup(hostinfo_holder->hostname); CHECKMEM(hostinfo_holder->addressinfo_tail->address); // add a 2nd to the list hostinfo_holder->addressinfo_tail->next = calloc(1, sizeof(addressinfo_holder_t)); CHECKMEM(hostinfo_holder->addressinfo_tail); hostinfo_holder->addressinfo_tail = hostinfo_holder->addressinfo_tail->next; // second is address hostinfo_holder->addressinfo_tail->address = strdup(PQgetvalue(result, i, 1)); CHECKMEM(hostinfo_holder->addressinfo_tail->address); // one for hostname one for address hostinfo_holder->address_count = 2; } else { // permenant memory for address list -- stored for duration hostinfo_holder->addressinfo_tail->next = calloc(1, sizeof(addressinfo_holder_t)); CHECKMEM(hostinfo_holder->addressinfo_tail); hostinfo_holder->addressinfo_tail = hostinfo_holder->addressinfo_tail->next; // permenant memory for address list -- stored for duration hostinfo_holder->addressinfo_tail->address = strdup(PQgetvalue(result, i, 1)); CHECKMEM(hostinfo_holder->addressinfo_tail->address); hostinfo_holder->address_count++; } } // if we have any appliance specific hosts such as hadoop nodes add them to the hash table if (get_appliance_hosts_and_add_to_hosts(pool, htab)) { TR0(("Not an appliance: checking for SW Only hadoop hosts.\n")); get_hadoop_hosts_and_add_to_hosts(pool, htab, opt); // Not an appliance, so check for SW only hadoop nodes. } unique_hosts = apr_hash_count(htab); // allocate memory for host list (not freed ever) hosts = calloc(unique_hosts, sizeof(host_t)); apr_hash_index_t* hi; void* vptr; int hostcounter = 0; for (hi = apr_hash_first(0, htab); hi; hi = apr_hash_next(hi)) { // sanity check if (hostcounter >= unique_hosts) { gpmon_fatalx(FLINE, 0, "host counter exceeds unique hosts"); } apr_hash_this(hi, 0, 0, &vptr); hostinfo_holder = vptr; hosts[hostcounter].hostname = strdup(hostinfo_holder->hostname); hosts[hostcounter].data_dir = strdup(hostinfo_holder->datadir); if (hostinfo_holder->smon_dir) { hosts[hostcounter].smon_bin_location = strdup(hostinfo_holder->smon_dir); } hosts[hostcounter].is_master = hostinfo_holder->is_master; hosts[hostcounter].addressinfo_head = hostinfo_holder->addressinfo_head; hosts[hostcounter].addressinfo_tail = hostinfo_holder->addressinfo_tail; hosts[hostcounter].address_count = hostinfo_holder->address_count; hosts[hostcounter].connection_hostname.current = hosts[hostcounter].addressinfo_head; hosts[hostcounter].snmp_hostname.current = hosts[hostcounter].addressinfo_head; if (hostinfo_holder->is_hdm) hosts[hostcounter].is_hdm = 1; if (hostinfo_holder->is_hdw) hosts[hostcounter].is_hdw = 1; if (hostinfo_holder->is_etl) hosts[hostcounter].is_etl = 1; if (hostinfo_holder->is_hbw) hosts[hostcounter].is_hbw = 1; if (hostinfo_holder->is_hdc) hosts[hostcounter].is_hdc = 1; apr_thread_mutex_create(&hosts[hostcounter].mutex, APR_THREAD_MUTEX_UNNESTED, global_pool); // use the global pool so the mutexes last beyond this function hostcounter++; } *hostcnt = hostcounter; } apr_pool_destroy(pool); PQclear(result); PQfinish(conn); if (!hosts || *hostcnt < 1) { gpmon_fatalx(FLINE, 0, "no valid hosts found"); } *host_table = hosts; }
static void gx_gettcpcmd(SOCKET sock, short event, void* arg) { char dump; int n, e; apr_pool_t* oldpool; apr_hash_t* qetab; apr_hash_t* qdtab; apr_hash_t* pidtab; apr_hash_t* segtab; if (event & EV_TIMEOUT) // didn't get command from gpmmon, quit { if(gx.tcp_sock) { close(gx.tcp_sock); gx.tcp_sock=0; } return; } apr_hash_t* querysegtab; n = recv(sock, &dump, 1, 0); if (n == 0) gx_exit("peer closed"); if (n == -1) gx_exit("socket error"); if (dump != 'D') gx_exit("bad data"); TR1(("start dump %c\n", dump)); qetab = gx.qexectab; qdtab = gx.qlogtab; pidtab = gx.pidtab; segtab = gx.segmenttab; querysegtab = gx.querysegtab; oldpool = apr_hash_pool_get(qetab); /* make new hashtabs for next cycle */ { apr_pool_t* newpool; if (0 != (e = apr_pool_create_alloc(&newpool, gx.pool))) { gpsmon_fatalx(FLINE, e, "apr_pool_create_alloc failed"); } /* qexec hash table */ gx.qexectab = apr_hash_make(newpool); CHECKMEM(gx.qexectab); /* qlog hash table */ gx.qlogtab = apr_hash_make(newpool); CHECKMEM(gx.qlogtab); /* segment hash table */ gx.segmenttab = apr_hash_make(newpool); CHECKMEM(gx.segmenttab); /* queryseg hash table */ gx.querysegtab = apr_hash_make(newpool); CHECKMEM(gx.querysegtab); /* pidtab hash table */ gx.pidtab = apr_hash_make(newpool); CHECKMEM(gx.pidtab); } /* push out a metric of the machine */ send_machine_metrics(sock); send_fsinfo(sock); /* push out records */ { apr_hash_index_t* hi; gp_smon_to_mmon_packet_t* ppkt = 0; gp_smon_to_mmon_packet_t localPacketObject; pidrec_t* pidrec; int count = 0; apr_hash_t* query_cpu_table = NULL; for (hi = apr_hash_first(0, querysegtab); hi; hi = apr_hash_next(hi)) { void* vptr; apr_hash_this(hi, 0, 0, &vptr); ppkt = vptr; if (ppkt->header.pkttype != GPMON_PKTTYPE_QUERYSEG) continue; TR2(("sending magic %x, pkttype %d\n", ppkt->header.magic, ppkt->header.pkttype)); send_smon_to_mon_pkt(sock, ppkt); count++; } for (hi = apr_hash_first(0, segtab); hi; hi = apr_hash_next(hi)) { void* vptr; apr_hash_this(hi, 0, 0, &vptr); ppkt = vptr; if (ppkt->header.pkttype != GPMON_PKTTYPE_SEGINFO) continue; /* fill in hostname */ strncpy(ppkt->u.seginfo.hostname, gx.hostname, sizeof(ppkt->u.seginfo.hostname) - 1); ppkt->u.seginfo.hostname[sizeof(ppkt->u.seginfo.hostname) - 1] = 0; TR2(("sending magic %x, pkttype %d\n", ppkt->header.magic, ppkt->header.pkttype)); send_smon_to_mon_pkt(sock, ppkt); count++; } for (hi = apr_hash_first(0, qdtab); hi; hi = apr_hash_next(hi)) { void* vptr; apr_hash_this(hi, 0, 0, &vptr); ppkt = vptr; if (ppkt->header.pkttype != GPMON_PKTTYPE_QLOG) continue; TR2(("sending magic %x, pkttype %d\n", ppkt->header.magic, ppkt->header.pkttype)); send_smon_to_mon_pkt(sock, ppkt); count++; } for (hi = apr_hash_first(0, qetab); hi; hi = apr_hash_next(hi)) { gpmon_qexec_t* qexec; void *vptr; apr_hash_this(hi, 0, 0, &vptr); qexec = vptr; /* fill in _p_metrics */ pidrec = apr_hash_get(pidtab, &qexec->key.hash_key.pid, sizeof(qexec->key.hash_key.pid)); if (pidrec) { qexec->_p_metrics = pidrec->p_metrics; qexec->_cpu_elapsed = pidrec->cpu_elapsed; } else { memset(&qexec->_p_metrics, 0, sizeof(qexec->_p_metrics)); } /* fill in _hname */ strncpy(qexec->_hname, gx.hostname, sizeof(qexec->_hname) - 1); qexec->_hname[sizeof(qexec->_hname) - 1] = 0; if (0 == create_qexec_packet(qexec, &localPacketObject)) { break; } TR2(("sending qexec, pkttype %d\n", localPacketObject.header.pkttype)); send_smon_to_mon_pkt(sock, &localPacketObject); count++; } // calculate CPU utilization per query for this machine query_cpu_table = apr_hash_make(oldpool); CHECKMEM(query_cpu_table); // loop through PID's and add to Query CPU Hash Table for (hi = apr_hash_first(0, pidtab); hi; hi = apr_hash_next(hi)) { void* vptr; pidrec_t* lookup; apr_hash_this(hi, 0, 0, &vptr); pidrec = vptr; TR2(("tmid %d ssid %d ccnt %d pid %d (CPU elapsed %d CPU Percent %.2f)\n", pidrec->query_key.tmid, pidrec->query_key.ssid, pidrec->query_key.ccnt, pidrec->pid, pidrec->cpu_elapsed, pidrec->p_metrics.cpu_pct)); // table is keyed on query key lookup = apr_hash_get(query_cpu_table, &pidrec->query_key, sizeof(pidrec->query_key)); if (lookup) { // found other pids with same query key so add the metrics to that lookup->cpu_elapsed += pidrec->cpu_elapsed; lookup->p_metrics.cpu_pct += pidrec->p_metrics.cpu_pct; } else { // insert existing pid record into table keyed by query key apr_hash_set(query_cpu_table, &pidrec->query_key, sizeof(pidrec->query_key), pidrec); } } // reset packet to 0 ppkt = &localPacketObject; memset(ppkt, 0, sizeof(gp_smon_to_mmon_packet_t)); gp_smon_to_mmon_set_header(ppkt,GPMON_PKTTYPE_QUERY_HOST_METRICS); // add the hostname into the packet for DEBUGGING purposes only. This is not used strncpy(ppkt->u.qlog.user, gx.hostname, sizeof(ppkt->u.qlog.user) - 1); ppkt->u.qlog.user[sizeof(ppkt->u.qlog.user) - 1] = 0; // loop through the query per cpu table and send the metrics for (hi = apr_hash_first(0, query_cpu_table); hi; hi = apr_hash_next(hi)) { void* vptr; apr_hash_this(hi, 0, 0, &vptr); pidrec = vptr; ppkt->u.qlog.key.tmid = pidrec->query_key.tmid; ppkt->u.qlog.key.ssid = pidrec->query_key.ssid; ppkt->u.qlog.key.ccnt = pidrec->query_key.ccnt; ppkt->u.qlog.cpu_elapsed = pidrec->cpu_elapsed; ppkt->u.qlog.p_metrics.cpu_pct = pidrec->p_metrics.cpu_pct; TR2(("SEND tmid %d ssid %d ccnt %d (CPU elapsed %d CPU Percent %.2f)\n", ppkt->u.qlog.key.tmid, ppkt->u.qlog.key.ssid, ppkt->u.qlog.key.ccnt, ppkt->u.qlog.cpu_elapsed, ppkt->u.qlog.p_metrics.cpu_pct)); send_smon_to_mon_pkt(sock, ppkt); count++; } TR1(("end dump ... sent %d entries\n", count)); } /* get rid of the old pool */ { apr_pool_destroy(oldpool); } struct timeval tv; tv.tv_sec = opt.terminate_timeout; tv.tv_usec = 0; if (event_add(&gx.tcp_event, &tv)) //reset timeout { gpmon_warningx(FLINE, APR_FROM_OS_ERROR(errno), "event_add failed"); } return; }
/* filalloc */ filamentptr filalloc(int nmax) { int i; filamentptr fil; fil=NULL; CHECKMEM(fil=(filamentptr) malloc(sizeof(struct filamentstruct))); fil->filss=NULL; fil->fname=NULL; fil->color[0]=fil->color[1]=fil->color[2]=0; fil->color[3]=1; fil->edgepts=1; fil->edgestipple[0]=1; fil->edgestipple[1]=0xFFFF; fil->drawmode=DMedge; fil->shiny=0; fil->nmax=nmax; fil->n=0; fil->front=0; fil->back=0; fil->px=NULL; fil->pl=NULL; fil->pa=NULL; fil->pd=NULL; fil->po=NULL; fil->pthk=NULL; fil->lstd=1.0; fil->astd[0]=0; fil->astd[1]=0; fil->astd[2]=0; fil->lk=1; fil->ak[0]=1; fil->ak[1]=1; fil->ak[2]=1; fil->kT=0; fil->treadrate=0; fil->surf=' '; fil->spar[0]=0; fil->spar[1]=0; CHECKMEM(fil->px=(double**)calloc(nmax+1,sizeof(double*))); for(i=0;i<=nmax;i++) fil->px[i]=NULL; for(i=0;i<=nmax;i++) CHECK(fil->px[i]=(double*)calloc(3,sizeof(double))); for(i=0;i<=nmax;i++) fil->px[i][0]=fil->px[i][1]=fil->px[i][2]=0; CHECKMEM(fil->pl=(double*)calloc(nmax,sizeof(double))); for(i=0;i<nmax;i++) fil->pl[i]=1; CHECKMEM(fil->pa=(double**)calloc(nmax,sizeof(double*))); for(i=0;i<nmax;i++) fil->pa[i]=NULL; for(i=0;i<nmax;i++) CHECK(fil->pa[i]=(double*)calloc(3,sizeof(double))); for(i=0;i<nmax;i++) fil->pa[i][0]=fil->pa[i][1]=fil->pa[i][2]=0; CHECKMEM(fil->pd=(double**)calloc(nmax,sizeof(double*))); for(i=0;i<nmax;i++) fil->pd[i]=NULL; for(i=0;i<nmax;i++) CHECK(fil->pd[i]=(double*)calloc(9,sizeof(double))); for(i=0;i<nmax;i++) Sph_One2Dcm(fil->pd[i]); CHECKMEM(fil->po=(double**)calloc(nmax,sizeof(double*))); for(i=0;i<nmax;i++) fil->po[i]=NULL; for(i=0;i<nmax;i++) CHECK(fil->po[i]=(double*)calloc(9,sizeof(double))); for(i=0;i<nmax;i++) Sph_One2Dcm(fil->po[i]); CHECKMEM(fil->pthk=(double*)calloc(nmax,sizeof(double))); for(i=0;i<nmax;i++) fil->pthk[i]=1; return fil; failure: if(fil) filfree(fil); return NULL; }