summaryrefslogtreecommitdiff
path: root/src/field_stat.cpp
blob: 2fc7920611e75b6bc2a1abdafe10e661346a0c81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "field_stat.h"
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#define	MAX_STAT_FIELD_NUM	256
#define	FIELD_PER_LINE		8
struct stat_field_t
{
	char	*filed_name;
	long long current_value;
	long long last_output_value;
};
struct screen_stat_space_t
{
	int stat_cycle;
	int screen_print_trigger;
	FILE* fp;
	struct stat_field_t stat_array[MAX_STAT_FIELD_NUM];
	int inuse_num;
};
void *thread_screen_print(void *arg)
{
	struct screen_stat_space_t* handle=(struct screen_stat_space_t*)arg;
	int i=0,j=0;
	long long differenc_valule;
	time_t now;
	char print_buf[1024*64]={0};
	char*pos=NULL;
	while(1)
	{
		now=time(NULL);
		pos=print_buf;
		pos+=snprintf(pos,sizeof(print_buf)-(pos-print_buf),"=====================================================%s",ctime(&now));
		while(i<handle->inuse_num)
		{
			pos+=snprintf(pos,sizeof(print_buf)-(pos-print_buf),"\t");
			for(j=0;j<FIELD_PER_LINE&&i+j<handle->inuse_num;j++)
			{
				pos+=snprintf(pos,sizeof(print_buf)-(pos-print_buf),"%10s\t",handle->stat_array[i+j].filed_name);
			}
			pos+=snprintf(pos,sizeof(print_buf)-(pos-print_buf),"\nsum\t");
			for(j=0;j<FIELD_PER_LINE&&i+j<handle->inuse_num;j++)
			{
				pos+=snprintf(pos,sizeof(print_buf)-(pos-print_buf),"%10lld\t",handle->stat_array[i+j].current_value);	
			}
			pos+=snprintf(pos,sizeof(print_buf)-(pos-print_buf),"\nspeed/s\t");
			for(j=0;j<FIELD_PER_LINE&&i+j<handle->inuse_num;j++)
			{
				differenc_valule=handle->stat_array[i+j].current_value-handle->stat_array[i+j].last_output_value;
				handle->stat_array[i+j].last_output_value=handle->stat_array[i+j].current_value;
				pos+=snprintf(pos,sizeof(print_buf)-(pos-print_buf),"%10lld\t",differenc_valule/handle->stat_cycle);	
			}
			i+=j;
			pos+=snprintf(pos,sizeof(print_buf)-(pos-print_buf),"\n");
		}
		fprintf(handle->fp,"%s",print_buf);
		i=0;
		sleep(handle->stat_cycle);
	}
	return NULL;
}
screen_stat_handle_t init_screen_stat(FILE* output_fp,int stat_cycle,int screen_print_trigger)
{
	struct screen_stat_space_t* handle=(struct screen_stat_space_t*)calloc(sizeof(struct screen_stat_space_t),1);
	handle->fp=output_fp;
	handle->stat_cycle=stat_cycle;
	handle->screen_print_trigger=screen_print_trigger;
	pthread_t cfg_mon_t;
	if(handle->screen_print_trigger==1)
	{
		pthread_create(&cfg_mon_t, NULL, thread_screen_print, (void*)handle);
	}
	return handle;
}

int stat_field_register(screen_stat_handle_t handle,const char* field_name)
{
	struct screen_stat_space_t* _handle=(struct screen_stat_space_t*)handle;
	int idx=_handle->inuse_num++;
	_handle->stat_array[idx].filed_name=(char*)calloc(strlen(field_name)+1,1);
	memcpy(_handle->stat_array[idx].filed_name,field_name,strlen(field_name));
	return idx;
}

int stat_field_operation(screen_stat_handle_t handle,int field_id,int operation,long long value)
{
	struct screen_stat_space_t* _handle=(struct screen_stat_space_t*)handle;
	switch(operation)
	{
		case	FS_OP_TYPE_ADD:
			_handle->stat_array[field_id].current_value+=value;
			break;
		case	FS_OP_TYPE_SET:
			_handle->stat_array[field_id].current_value=value;
			break;	
		default:
			return -1;
			break;
	}
	return 0;
}