Beispiel #1
0
/*
 * This is the function called from the main polling loop.
 *
 * It calls command functions to get strings of data, and sends them to the server.
 *
*/
int collect_and_send_metrics(int cycle) {
	int retval;
	char* command;
	
	StringInfoData commands;
	
	pgstat_report_activity(STATE_RUNNING, "Collecting metrics");
	
	initStringInfo(&commands);
	/*
	 * Populate first cycle command data.	These are executed on the first cycle
	 *	 of a restart.	The bgworker restars every N cycles, as listed at the bottom
	 *	 of the main loop in pgsampler.c.	
	 */
	if (cycle == 0) {
	
		command = restart_gucs();
		appendStringInfoString(&commands, command);
		if (strcmp(output_mode, "csv") == 0) 
			write_to_csv("restart_gucs", command);			
		pfree(command);

		command = relation_info();
		appendStringInfoString(&commands, command);
		if (strcmp(output_mode, "csv") == 0) 
			write_to_csv("stat_class", command);
		pfree(command);

		command = database_list();
		appendStringInfoString(&commands, command);
		if (strcmp(output_mode, "csv") == 0) 
			write_to_csv("databases", command);
		pfree(command);
		
		command = column_info();
		appendStringInfoString(&commands, command);
		if (strcmp(output_mode, "csv") == 0) 
			write_to_csv("columns", command);
		pfree(command);

		command = index_info();
		appendStringInfoString(&commands, command);
		if (strcmp(output_mode, "csv") == 0) 
			write_to_csv("indexes", command);
		pfree(command);

		command = column_stats();
		appendStringInfoString(&commands, command);
		if (strcmp(output_mode, "csv") == 0) 
			write_to_csv("stat_column", command);
		pfree(command);

		command = db_stats();
		appendStringInfoString(&commands, command);
		if (strcmp(output_mode, "csv") == 0) 
			write_to_csv("stat_database", command);
		pfree(command);

	}

	/* HEARTBEAT */
	if (cycle % heartbeat_seconds == 0) {
		command = heartbeat();
		appendStringInfoString(&commands, command);
		if (strcmp(output_mode, "csv") == 0) 
			write_to_csv("heartbeats", command);
		pfree(command);
	}

	/* SYSTEM INFO */
	if (cycle % system_seconds == 0) {
		command = system_info();	
		appendStringInfoString(&commands, command);
		if (strcmp(output_mode, "csv") == 0) 
			write_to_csv("stat_system", command);
		pfree(command);

		command = fs_info();
		appendStringInfoString(&commands, command);
		if (strcmp(output_mode, "csv") == 0) 
			write_to_csv("stat_filesystem", command);
		pfree(command);
	}

	/* */
	if (cycle % activity_seconds == 0) {
		command = activity_stats(); 
		appendStringInfoString(&commands, command);
		if (strcmp(output_mode, "csv") == 0) 
			write_to_csv("stat_activity", command);
		pfree(command);
	}

	if (cycle % replication_seconds == 0) {
		command = replication_stats(); 
		appendStringInfoString(&commands, command);
		if (strcmp(output_mode, "csv") == 0) 
			write_to_csv("stat_replication", command);
		pfree(command);
	}

	/*	*/
	if (cycle % bgwriter_seconds == 0) {
		command = bgwriter_stats();
		appendStringInfoString(&commands, command);
		if (strcmp(output_mode, "csv") == 0) 
			write_to_csv("stat_bgwriter", command);
		pfree(command);
	}
	
	if (cycle % guc_seconds == 0) {
		command = transient_gucs();
		appendStringInfoString(&commands, command);
		if (strcmp(output_mode, "csv") == 0) 
			write_to_csv("transient_gucs", command);
		pfree(command);
	}
	
	if (cycle % statements_seconds == 0) {
		command = stat_statements();
		appendStringInfoString(&commands, command);
		if (strcmp(output_mode, "csv") == 0) 
			write_to_csv("stat_statements", command);
		pfree(command);
	}

	/* */	
	if (cycle % relation_seconds == 0) {
		command = table_stats();
		appendStringInfoString(&commands, command);
		if (strcmp(output_mode, "csv") == 0) 
			write_to_csv("stat_table", command);
		pfree(command);
		
		command = index_stats();
		appendStringInfoString(&commands, command);
		if (strcmp(output_mode, "csv") == 0) 
			write_to_csv("stat_index", command);
		pfree(command);
		
		command = table_io_stats();
		appendStringInfoString(&commands, command);
		if (strcmp(output_mode, "csv") == 0) 
			write_to_csv("statio_user_tables", command);
		pfree(command);

		command = index_io_stats();
		appendStringInfoString(&commands, command);
		if (strcmp(output_mode, "csv") == 0) 
			write_to_csv("statio_user_indexes", command);
		pfree(command);

		command = function_stats();
		appendStringInfoString(&commands, command);
		if (strcmp(output_mode, "csv") == 0) 
			write_to_csv("stat_function", command);
		pfree(command);
	}



	/* Send / Write metrics based on output_mode */
	if (strcmp(output_mode, "network") == 0) {
		pgstat_report_activity(STATE_RUNNING, "Sending metrics to antenna");
		retval = send_data(commands.data);
		if (retval == NO_DATA_SENT) { //close socket and retry establishing connection and sending data.
		 	// elog(LOG, "reseting..."); //just a note to say reseting socket
			if (sockfd != 0) 
				shutdown(sockfd, SHUT_RDWR);

			sockfd = 0;
			retval = send_data(commands.data); // we ignore success or failure here.	drops data if fails.
		}
	}

	return 0;
}
Beispiel #2
0
/* internal function to execute queries from a topic file against an
 * index and output the results in trec_eval format */
static int process_topic_file(FILE *fp, struct args *args, FILE *output, 
  struct treceval *teresults) {
    struct index_stats stats;
    char *query,
         *querynum = NULL;
    struct index_result *results = NULL;
    unsigned int returned,
                 i;
    int est;
    double total_results;
    struct mlparse_wrap *parser;
    struct timeval now, then;
    struct timeval topic_now, topic_then;

    gettimeofday(&topic_then, NULL);

    if (!index_stats(args->idx, &stats)) {
        return 0;
    }

    if ((parser = mlparse_wrap_new_file(stats.maxtermlen, LOOKAHEAD, fp, 
        BUFSIZ, 0))
      && (results = malloc(sizeof(*results) * args->numresults))
      && (querynum = malloc(stats.maxtermlen + 2))) {

        while ((query = get_next_query(parser, 
            querynum, stats.maxtermlen + 1, args->title, args->descr, 
            args->narr, &args->sopt.word_limit, args))) {

            /* check that we actually got a query */
            if (!str_len(query)) {
                if (!args->cont) {
                    fprintf(stderr, "failed to extract query for topic %s\n", 
                      querynum);
                }
                if (atoi((const char *) querynum) == 201) {
                    fprintf(stderr, "looks like it occurred on TREC topics "
                      "201-250, which is probably because you specified a "
                      "title-only run and it doesn't contain titles\n");
                }

                if (args->cont && strlen(querynum)) {
                    /* continue evaluation */
                    if (args->dummy) {
                        /* no results, insert dummy result if requested */
                        fprintf(output, "%s\tQ0\t%s\t%u\t%f\t%s\n", querynum,
                          "XXXX-XXX-XXXXXXX", 1, 0.0, args->run_id);
                    }
                } else {
                    /* give up evaluation */
                    free(query);
                    free(results);
                    free(querynum);
                    mlparse_wrap_delete(parser);
                    return 0;
                }
            }
 
            args->sopt.summary_type = INDEX_SUMMARISE_NONE;

            /* FIXME: detect errors */

            gettimeofday(&then, NULL);
            if (index_search(args->idx, (const char *) query, 0, 
                args->numresults, results, &returned, 
                &total_results, &est, (args->sopts | INDEX_SEARCH_WORD_LIMIT | 
                INDEX_SEARCH_SUMMARY_TYPE), &args->sopt)) {
                char aux_buf[512];

                gettimeofday(&now, NULL);

                if (args->print_queries) {
                    //fprintf(stderr,
                    // sengor
                    printf( 
                      "query '%s' completed in %lu microseconds\n", query,
                      (unsigned long int) now.tv_usec - then.tv_usec 
                        + (now.tv_sec - then.tv_sec) * 1000000);
                }

                /* print results */
                for (i = 0; i < returned; i++) {
                    char *docno = NULL;

                    /* hack: any document that doesn't have a TREC docno will
                     * inherit the docno of the previous document (until we 
                     * find one that has a docno) */

                    if (results[i].auxilliary && results[i].auxilliary[0]) {
                        strncpy(aux_buf, results[i].auxilliary, 
                          sizeof(aux_buf));
                        docno = aux_buf;
                    } else {
                        int ret = 1;
                        unsigned long int docnum = results[i].docno;
                        unsigned int aux_len = 0;

                        /* retrieve TREC docno for progressively higher docs */
                        while (ret && (docnum > 0) && !aux_len) {
                            docnum--;
                            ret = index_retrieve_doc_aux(args->idx, docnum,
                              aux_buf, sizeof(aux_buf) - 1, &aux_len);
                            /* XXX assume trec docno always < 512 bytes long */
                        }
                        if (ret && aux_len) {
                            docno = aux_buf;
                        }
                    }
                    aux_buf[sizeof(aux_buf) - 1] = '\0';

                    if (teresults 
                      && docno
                      && treceval_add_result(teresults, atoi(querynum), 
                          docno, (float) results[i].score)) {
                        /* they want evaluated results, stuck it in the results
                         * structure */
                    } else if (!teresults && docno) {
                        str_toupper(docno);

                        /* print out query_id, iter (ignored - so we print
                         * out number of seconds taken), docno, rank
                         * (ignored), score, run_id */
                        fprintf(output, "%s\tQ0\t%s\t%u\t%f\t%s\n", querynum, 
                          docno, i + 1, results[i].score, 
                          args->run_id);
                    } else if (teresults && docno) {
                        fprintf(stderr, "failed to add to treceval results\n");
                        free(results);
                        free(querynum);
                        free(docno);
                        free(query);
                        mlparse_wrap_delete(parser);
                        return 0;
                    } else {
                        /* couldn't copy the docno */
                        fprintf(stderr, "docno ('%s') copy failed: %s\n", 
                          results[i].auxilliary, strerror(errno));
                        free(query);
                        free(results);
                        free(querynum);
                        mlparse_wrap_delete(parser);
                        return 0;
                    }
                }

                if (returned == 0 && args->dummy) {
                    /* no results, insert dummy result if requested */
                    fprintf(output, "%s\tQ0\t%s\t%u\t%f\t%s\n", querynum,    
                      "XXXX-XXX-XXXXXXX", 1, 0.0, args->run_id);
                }
            } else {
                /* error searching */
                fprintf(stderr, "error searching index\n");
                free(query);
                mlparse_wrap_delete(parser);
                return 0;
            }

            free(query);
            querynum[0] = '\0';
        }

        if (!feof(fp)) {
            fprintf(stderr, "parser or read error\n");
            free(results);
            free(querynum);
            mlparse_wrap_delete(parser);
            return 0;
        }
        free(results);
        free(querynum);

        gettimeofday(&topic_now, NULL);

        if (args->timing) {
            fprintf(stderr, "topic processed in %lu microseconds\n", 
              (unsigned long int) topic_now.tv_usec - topic_then.tv_usec 
                + (topic_now.tv_sec - topic_then.tv_sec) * 1000000);
        }

        mlparse_wrap_delete(parser);
        return 1;
    } else {
        if (parser) {
            if (results) {
                free(results);
            }
            mlparse_wrap_delete(parser);
        }
    }

    return 0;
}