Exemple #1
0
Fichier : pg.c Projet : xrg/pg_top
void
pg_display_table_stats(char *conninfo, int compare_index, int max_topn)
{
	int			i;
	int			rows;
	PGconn	   *pgconn;
	PGresult   *pgresult = NULL;
	static char line[512];

	static struct table_node *head = NULL;
	static struct table_node **procs = NULL;

	int			max_lines;

	/* Get the currently running query. */
	pgconn = connect_to_db(conninfo);
	if (pgconn != NULL)
	{
		pgresult = PQexec(pgconn, SELECT_TABLE_STATS);
		rows = PQntuples(pgresult);
	}
	else
	{
		PQfinish(pgconn);
		return;
	}
	PQfinish(pgconn);

	max_lines = rows < max_topn ? rows : max_topn;

	procs = (struct table_node **) realloc(procs,
										 rows * sizeof(struct table_node *));
	/* Calculate change in values. */
	for (i = 0; i < rows; i++)
	{
		head = upsert_table_stats(head,
								  atoll(PQgetvalue(pgresult, i, 0)),
								  atoll(PQgetvalue(pgresult, i, 2)),
								  atoll(PQgetvalue(pgresult, i, 3)),
								  atoll(PQgetvalue(pgresult, i, 4)),
								  atoll(PQgetvalue(pgresult, i, 5)),
								  atoll(PQgetvalue(pgresult, i, 6)),
								  atoll(PQgetvalue(pgresult, i, 7)),
								  atoll(PQgetvalue(pgresult, i, 8)));
	}

	/* Sort stats. */
	for (i = 0; i < rows; i++)
	{
		procs[i] = get_table_stats(head, atoll(PQgetvalue(pgresult, i, 0)));
		procs[i]->name_index = i;
	}
	qsort(procs, rows, sizeof(struct table_node *),
		  table_compares[compare_index]);

	for (i = rows - 1; i > rows - max_lines - 1; i--)
	{
		if (mode_stats == STATS_DIFF) {
			snprintf(line, sizeof(line),
				 	"%9lld %9lld %9lld %9lld %9lld %9lld %9lld %s",
				 	procs[i]->diff_seq_scan,
				 	procs[i]->diff_seq_tup_read,
				 	procs[i]->diff_idx_scan,
				 	procs[i]->diff_idx_tup_fetch,
				 	procs[i]->diff_n_tup_ins,
				 	procs[i]->diff_n_tup_upd,
				 	procs[i]->diff_n_tup_del,
				 	PQgetvalue(pgresult, procs[i]->name_index, 1));
		}
		else
		{
			snprintf(line, sizeof(line),
				 	"%9lld %9lld %9lld %9lld %9lld %9lld %9lld %s",
				 	procs[i]->total_seq_scan,
				 	procs[i]->total_seq_tup_read,
				 	procs[i]->total_idx_scan,
				 	procs[i]->total_idx_tup_fetch,
				 	procs[i]->total_n_tup_ins,
				 	procs[i]->total_n_tup_upd,
				 	procs[i]->total_n_tup_del,
				 	PQgetvalue(pgresult, procs[i]->name_index, 1));
		}
		u_process(rows - i - 1, line);
	}

	if (pgresult != NULL)
		PQclear(pgresult);
}
Exemple #2
0
void
do_display(globalstate *gstate)

{
    int active_procs;
    int i;
    time_t curr_time;
    caddr_t processes;
    struct system_info system_info;
    char *hdr;

    /* get the time */
    time_mark(&(gstate->now));
    curr_time = (time_t)(gstate->now.tv_sec);

    /* get the current stats */
    get_system_info(&system_info);

    /* get the current processes */
    processes = get_process_info(&system_info, &(gstate->pselect), gstate->order_index);

    /* determine number of processes to actually display */
    if (gstate->topn > 0)
    {
	/* this number will be the smallest of:  active processes,
	   number user requested, number current screen accomodates */
	active_procs = system_info.P_ACTIVE;
	if (active_procs > gstate->topn)
	{
	    active_procs = gstate->topn;
	}
	if (active_procs > gstate->max_topn)
	{
	    active_procs = gstate->max_topn;
	}
    }
    else
    {
	/* dont show any */
	active_procs = 0;
    }

    hdr = gstate->header_text;

    /* full screen or update? */
    if (gstate->fulldraw)
    {
	display_clear();
	i_loadave(system_info.last_pid, system_info.load_avg);
	i_uptime(&(gstate->statics->boottime), &curr_time);
	i_timeofday(&curr_time);
	i_procstates(system_info.p_total, system_info.procstates, gstate->pselect.threads);
	if (gstate->show_cpustates)
	{
	    i_cpustates(system_info.cpustates);
	}
	else
	{
	    if (smart_terminal)
	    {
		z_cpustates();
	    }
	    gstate->show_cpustates = Yes;
	}
	i_kernel(system_info.kernel);
	i_memory(system_info.memory);
	i_swap(system_info.swap);
	i_message(&(gstate->now));
	i_header(hdr);
	for (i = 0; i < active_procs; i++)
	{
	    i_process(i, format_next_process(processes, gstate->get_userid));
	}
	i_endscreen();
	if (gstate->smart_terminal)
	{
	    gstate->fulldraw = No;
	}
    }
    else
    {
	u_loadave(system_info.last_pid, system_info.load_avg);
	u_uptime(&(gstate->statics->boottime), &curr_time);
	i_timeofday(&curr_time);
	u_procstates(system_info.p_total, system_info.procstates, gstate->pselect.threads);
	u_cpustates(system_info.cpustates);
	u_kernel(system_info.kernel);
	u_memory(system_info.memory);
	u_swap(system_info.swap);
	u_message(&(gstate->now));
	u_header(hdr);
	for (i = 0; i < active_procs; i++)
	{
	    u_process(i, format_next_process(processes, gstate->get_userid));
	}
	u_endscreen();
    }
}