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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
|
/*
���ļ���ƽ̨���Ĺ��ܺͺ���(Ip��Ƭ, ����ԭ, ��������, ���������),
���ṩ������ҵ�����Ľӿ�, ��sapp_get_platform_opt��, ��Ϊһ��.so�ļ����ص�ƽ̨,
���ڸ��²���.
*/
#include "sapp_api.h"
#include "sapp_private_api.h"
#include "field_stat2.h"
static int sapp_fs2_init(void);
static int sapp_line_protocol_init(void);
#ifdef __cplusplus
extern "C" {
#endif
//extern time_t g_CurrentTime;//add by lqy 20070606
extern volatile unsigned long long g_SysInputInfo[MAX_THREAD_NUM][COUNTER_NUM];
//extern int g_packet_io_thread_num;
extern char g_app_instance_name[64];
//extern int g_timestamp_record_sw;
static pthread_mutex_t g_plug_Independent_thread_mutex;
static pthread_t *g_plug_Independent_thread_pid;
static const unsigned char G_BROADCAST_ADDR[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
int sapp_assistant_version_VERSION_20181024;
extern int MESA_get_dev_ipv4(const char *device, int *ip_add);
extern int MESA_get_dev_mac(const char *device, unsigned char mac[6]);
void * (*dl_marsio_buff_ctrlzone)(void *m, uint8_t id);
static int sapp_identify_broad_multicast_init(void)
{
#if IOMODE_MARSIO
void *io_lib_handle;
if(CAP_MODEL_MARSIOV4 == g_packet_io_cap_mode){
#if LINK_MODE_DYNAMIC
io_lib_handle = dlopen("./platform_lib/packet_io_marsio.so", RTLD_NOW | RTLD_GLOBAL);
if(NULL == io_lib_handle){
printf("\033[1;31;40m[Error]dlopen %s error, %s!\033[0m\n", "./platform_lib/packet_io_marsio.so", dlerror());
return -1;
}
dl_marsio_buff_ctrlzone = (void * (*)(void *m, uint8_t id))dlsym(io_lib_handle, "marsio_buff_ctrlzone");
if(NULL == dl_marsio_buff_ctrlzone){
printf("\033[1;31;40m[Error]dlsym %s error, %s!\033[0m\n", "marsio_buff_ctrlzone", dlerror());
return -1;
}
/* �˴���Ҫ��dlclose(), ��Ϊ������Ҫ��dlopen��packet_io_marsio.so, */
#else
dl_marsio_buff_ctrlzone = (void * (*)(void *m, uint8_t id))marsio_buff_ctrlzone;
#endif
}
#endif
return 0;
}
/* ʶ���Ƿ������ڲ����ݰ� , �����ڵİ�Ӧ��������ת��/��ע */
int sapp_identify_tunnel_inner_pkt(const raw_pkt_t *raw_pkt)
{
#if IOMODE_MARSIO
if(CAP_MODEL_MARSIOV4 == g_packet_io_cap_mode){
const struct mr_tunnat_ctrlzone *mr_ctzone;
if(dl_marsio_buff_ctrlzone){
mr_ctzone = (const struct mr_tunnat_ctrlzone *)((*dl_marsio_buff_ctrlzone)((void *)raw_pkt->io_lib_pkt_reference, 0));
if(mr_ctzone->action & TUNNAT_CZ_ACTION_FORWARD){
return 1;
}
}
}
#endif
return 0;
}
int sapp_identify_broad_multicast_pkt(const void *this_layer_data, const raw_pkt_t *raw_pkt)
{
const struct mesa_ethernet_hdr *p_eth_hdr = (const struct mesa_ethernet_hdr *)this_layer_data;
unsigned short eth_type = ntohs(p_eth_hdr->ether_type);
if(sapp_identify_tunnel_inner_pkt(raw_pkt) != 0){
return 0;
}
if(memcmp(G_BROADCAST_ADDR, p_eth_hdr->ether_dhost, 6) == 0){
/* G����ģʽ�£���ARPЭ��Ĺ㲥����ֱ�Ӷ���! ��ֹ�ٻ�ע��������, ��ɹ㲥�籩���������� */
if(eth_type != ETH_P_ARP){
return 1;
}
}else{
/* 2017-10-10 lijia add, �鲥MAC��ַ, ͨ��Ϊ�������ڿ��������ݰ�, ��LLMNR, STP��Э��, һ�����账�� */
if((p_eth_hdr->ether_dhost[0] & 0x01) == 0x01){
return 1;
}
}
return 0;
}
/*
ctype:
'c':count;
'l':length;
*/
static inline unsigned long long __get_platform_opt_traffic(int ctype, sapp_sys_stat_type_t index)
{
int i;
unsigned long long tmp_long = 0;
for(i = 0; i < g_packet_io_thread_num; i++){
if('c' == ctype){
tmp_long += sapp_global_val->mthread_volatile[i]->sys_stat.count[index];
}else{
tmp_long += sapp_global_val->mthread_volatile[i]->sys_stat.length[index];
}
}
return tmp_long;
}
/* 2017-09-04 lijia add, for ��������, �����ȡƽ̨�ڲ����� */
int sapp_get_platform_opt(enum sapp_platform_opt opt, void *opt_val, int *opt_val_len)
{
int i, ret = 0;
unsigned long long tmp_long;
if((NULL == opt_val) || (NULL == opt_val_len) || (*opt_val_len <= 0)){
return -1;
}
switch((int)opt){
case SPO_TOTAL_RCV_PKT:
{
if(*opt_val_len != sizeof(long long)){
ret = -1;
sapp_runtime_log(RLOG_LV_DEBUG, "sapp_get_platform_opt() error:opt_val_len:%d is invalid!\n", *opt_val_len);
break;
}
unsigned long long *tot_pkt = (unsigned long long *)opt_val;
*tot_pkt = __get_platform_opt_traffic('c', SAPP_STAT_RCV_ETHERNET);
}
break;
case SPO_TOTAL_RCV_BYTE:
{
if(*opt_val_len != sizeof(long long)){
ret = -1;
sapp_runtime_log(RLOG_LV_DEBUG, "sapp_get_platform_opt() error:opt_val_len:%d is invalid!\n", *opt_val_len);
break;
}
unsigned long long *tot_byte = (unsigned long long *)opt_val;
*tot_byte = *tot_byte = __get_platform_opt_traffic('l', SAPP_STAT_RCV_ETHERNET);;
}
break;
case SPO_TOTAL_INBOUND_PKT:
{
if(*opt_val_len != sizeof(long long)){
ret = -1;
sapp_runtime_log(RLOG_LV_DEBUG, "sapp_get_platform_opt() error:opt_val_len:%d is invalid!\n", *opt_val_len);
break;
}
unsigned long long *inbound_pkt = (unsigned long long *)opt_val;
*inbound_pkt = __get_platform_opt_traffic('c', SAPP_STAT_ETH_INBOUND);
}
break;
case SPO_TOTAL_INBOUND_BYTE:
{
if(*opt_val_len != sizeof(long long)){
ret = -1;
sapp_runtime_log(RLOG_LV_DEBUG, "sapp_get_platform_opt() error:opt_val_len:%d is invalid!\n", *opt_val_len);
break;
}
unsigned long long *inbound_byte = (unsigned long long *)opt_val;
*inbound_byte = __get_platform_opt_traffic('l', SAPP_STAT_ETH_INBOUND);
}
break;
case SPO_TOTAL_OUTBOUND_PKT:
{
if(*opt_val_len != sizeof(long long)){
ret = -1;
sapp_runtime_log(RLOG_LV_DEBUG, "sapp_get_platform_opt() error:opt_val_len:%d is invalid!\n", *opt_val_len);
break;
}
unsigned long long *outbound_pkt = (unsigned long long *)opt_val;
*outbound_pkt = __get_platform_opt_traffic('c', SAPP_STAT_ETH_OUTBOUND);
}
break;
case SPO_TOTAL_OUTBOUND_BYTE:
{
if(*opt_val_len != sizeof(long long)){
ret = -1;
sapp_runtime_log(RLOG_LV_DEBUG, "sapp_get_platform_opt() error:opt_val_len:%d is invalid!\n", *opt_val_len);
break;
}
unsigned long long *outbound_byte = (unsigned long long *)opt_val;
*outbound_byte = __get_platform_opt_traffic('l', SAPP_STAT_ETH_OUTBOUND);
}
break;
case SPO_TCP_STREAM_NEW:
{
if(*opt_val_len != sizeof(long long)){
ret = -1;
sapp_runtime_log(RLOG_LV_DEBUG, "sapp_get_platform_opt() error:opt_val_len:%d is invalid!\n", *opt_val_len);
break;
}
unsigned long long *tot_byte = (unsigned long long *)opt_val;
*tot_byte = *tot_byte = __get_platform_opt_traffic('c', SAPP_STAT_TCP_STREAM_NEW);
}
break;
case SPO_TCP_STREAM_ESTAB:
{
if(*opt_val_len != sizeof(long long)){
ret = -1;
sapp_runtime_log(RLOG_LV_DEBUG, "sapp_get_platform_opt() error:opt_val_len:%d is invalid!\n", *opt_val_len);
break;
}
unsigned long long *tot_byte = (unsigned long long *)opt_val;
*tot_byte = *tot_byte = __get_platform_opt_traffic('c', SAPP_STAT_TCP_STREAM_DATA);
}
break;
case SPO_TCP_STREAM_CLOSE:
{
if(*opt_val_len != sizeof(long long)){
ret = -1;
sapp_runtime_log(RLOG_LV_DEBUG, "sapp_get_platform_opt() error:opt_val_len:%d is invalid!\n", *opt_val_len);
break;
}
unsigned long long *tot_byte = (unsigned long long *)opt_val;
*tot_byte = *tot_byte = __get_platform_opt_traffic('c', SAPP_STAT_TCP_STREAM_DEL);
}
break;
case SPO_THREAD_COUNT:
{
if(*opt_val_len != sizeof(int)){
ret = -1;
sapp_runtime_log(RLOG_LV_DEBUG, "sapp_get_platform_opt() error:opt_val_len:%d is invalid!\n", *opt_val_len);
break;
}
int *tcnt = (int *)opt_val;
*tcnt = g_packet_io_thread_num;
}
break;
case SPO_CURTIME_TIMET:
{
if(*opt_val_len != sizeof(time_t)){
ret = -1;
sapp_runtime_log(RLOG_LV_DEBUG, "sapp_get_platform_opt() error:opt_val_len:%d is invalid!\n", *opt_val_len);
break;
}
time_t *curtime = (time_t *)opt_val;
*curtime = g_CurrentTime;
}
break;
case SPO_CURTIME_STRING:
{
if(*opt_val_len <= (int)strlen("1970-01-01 11:11:11")){
ret = -1;
sapp_runtime_log(RLOG_LV_DEBUG, "sapp_get_platform_opt() error:opt_val_len:%d is invalid!\n", *opt_val_len);
break;
}
time_t cur_time = g_CurrentTime;
struct tm date_loc;
int ret;
localtime_r(&cur_time, &date_loc);
ret = snprintf((char *)opt_val, *opt_val_len, "%04d-%02d-%02d %02d:%02d:%02d",
date_loc.tm_year+1900, date_loc.tm_mon+1,date_loc.tm_mday,
date_loc.tm_hour, date_loc.tm_min, date_loc.tm_sec);
*opt_val_len = ret;
}
break;
case SPO_START_TIME:
{
if(*opt_val_len != sizeof(time_t)){
ret = -1;
sapp_runtime_log(RLOG_LV_DEBUG, "sapp_get_platform_opt() error:opt_val_len:%d is invalid!\n", *opt_val_len);
break;
}
time_t *stime = (time_t *)opt_val;
*stime = ABBR_SAPP_START_TIME;
}
break;
case SPO_RUN_TIME:
{
if(*opt_val_len != sizeof(time_t)){
ret = -1;
sapp_runtime_log(RLOG_LV_DEBUG, "sapp_get_platform_opt() error:opt_val_len:%d is invalid!\n", *opt_val_len);
break;
}
time_t *runtime = (time_t *)opt_val;
*runtime = ABBR_CURRENT_TIME - ABBR_SAPP_START_TIME;
}
break;
/* �ⲿ���Ƶ������rand�Ⱥ����Ƚ�����CPU, ʹ��CPU��ǰ��ʱ��������, �͵�ǰƽ̨�����İ���ƴ��һ������� */
case SPO_RAND_NUMBER:
{
if(*opt_val_len != sizeof(long long)){
ret = -1;
sapp_runtime_log(RLOG_LV_DEBUG, "sapp_get_platform_opt() error:opt_val_len:%d is invalid!\n", *opt_val_len);
break;
}
long long *rnum = (long long *)opt_val;
*rnum = ((sapp_get_cpu_cycle() & 0xFFFF) << 48)
| ((g_SysInputInfo[0][PKT_TCPLEN] & 0xFFFF) << 32)
| ((g_SysInputInfo[0][PKT_IPLEN] & 0xFFFF) << 16)
| ((g_SysInputInfo[0][PKT_ETHERNET_LEN] & 0xFFFF));
}
break;
case SPO_FIELD_STAT_HANDLE:
{
if(*opt_val_len != sizeof(long long)){
ret = -1;
sapp_runtime_log(RLOG_LV_DEBUG, "sapp_get_platform_opt() error:opt_val_len:%d is invalid!\n", *opt_val_len);
break;
}
void **out_handle = (void **)opt_val;
*out_handle = sapp_global_val->individual_fixed.profiling_log_remote_handle;
}
break;
case SPO_INDEPENDENT_THREAD_ID:
{
int id_index = -1;
int *opt_int = (int *)opt_val;
int i;
pthread_t this_pid = pthread_self(); /* ��ȡ��ǰ�߳�id */
pthread_mutex_lock(&g_plug_Independent_thread_mutex);
/* Ϊ����һ���̶߳�ε��ô˽ӿ�, �Ȳ���֮ǰ�Ƿ�� */
for(i = 0; i < sapp_global_val->config.cpu.send_only_threads_max_num; i++){
if(g_plug_Independent_thread_pid[i] == this_pid){
id_index = i;
break;
}
}
if(-1 == id_index){ /* ��ǰ�̵߳�һ�δ���, û�ҵ� */
for(i = 0; i < sapp_global_val->config.cpu.send_only_threads_max_num; i++){
if(0 == g_plug_Independent_thread_pid[i]){
g_plug_Independent_thread_pid[i] = this_pid;
break;
}
}
if(i >= sapp_global_val->config.cpu.send_only_threads_max_num ){ /* ���������߳�������� */
sapp_runtime_log(RLOG_LV_FATAL, "Independent_thread num more than CPU->send_only_threads_max:%d!\n",
sapp_global_val->config.cpu.send_only_threads_max_num);
*opt_int = -1;
ret = -1;
}else{
*opt_int = g_packet_io_thread_num + i;
}
}else{
*opt_int = g_packet_io_thread_num + id_index;
}
pthread_mutex_unlock(&g_plug_Independent_thread_mutex);
}
break;
case SPO_DEPLOYMENT_MODE_STR:
{
if(*opt_val_len <= strlen(sapp_global_val->config.packet_io.depolyment_mode_str)){
ret = -1;
sapp_runtime_log(RLOG_LV_DEBUG, "sapp_get_platform_opt() SPO_DEPLOYMENT_MODE_STR error:opt_val_len:%d is not enough!\n", *opt_val_len);
break;
}
strncpy((char *)opt_val, sapp_global_val->config.packet_io.depolyment_mode_str, *opt_val_len);
}
break;
default:
sapp_runtime_log(RLOG_LV_DEBUG, "sapp_get_platform_opt() error:opt_type:%d not support!\n", (int)opt);
ret = -1;
}
return ret;
}
static int __sdo_get_dev_mtu(const char *device, int *mtu)
{
struct ifreq ifr;
int fd;
fd = socket(AF_INET, SOCK_DGRAM, 0);
if(fd < 0)
{
return -1;
}
memset(ifr.ifr_ifrn.ifrn_name, 0, sizeof(ifr.ifr_ifrn.ifrn_name));
strncpy(ifr.ifr_ifrn.ifrn_name, device, sizeof(ifr.ifr_ifrn.ifrn_name));
if(ioctl(fd, SIOCGIFMTU, &ifr) == -1)
{
//perror("Cann't get ip addr:");
goto err_exit;
}
*mtu = ifr.ifr_ifru.ifru_mtu;
close(fd);
return 0;
err_exit:
close(fd);
return -1;
}
int sapp_get_device_opt(const char *device, enum sapp_device_opt opt_type, void *opt_val, int *opt_val_len)
{
int ret;
unsigned char uchar_tmp_buf[8];
//char *char_out_value;
int int_out_value;
if((NULL == opt_val) || (NULL == opt_val_len) || (*opt_val_len <= 0)){
return -1;
}
switch((int)opt_type){
case SDO_MAC_ADDR:
{
ret = MESA_get_dev_mac(device, uchar_tmp_buf);
if(ret < 0){
return ret;
}
if(*opt_val_len < ETH_ALEN){
return -1;
}
memcpy(opt_val, uchar_tmp_buf, ETH_ALEN);
*opt_val_len = ETH_ALEN;
ret = 0;
}
break;
case SDO_IPV4_ADDR:
{
ret = MESA_get_dev_ipv4(device, &int_out_value);
if(ret < 0){
return ret;
}
if(*opt_val_len < (int)sizeof(int)){
return -1;
}
memcpy(opt_val, &int_out_value, sizeof(int));
*opt_val_len = (int)sizeof(int);
ret = 0;
}
break;
case SDO_MTU:
{
ret = __sdo_get_dev_mtu(device, &int_out_value);
if(ret < 0){
return ret;
}
if(*opt_val_len < (int)sizeof(int)){
return -1;
}
memcpy(opt_val, &int_out_value, sizeof(int));
*opt_val_len = (int)sizeof(int);
ret = 0;
}
break;
default:
sapp_runtime_log(RLOG_LV_FATAL, "sapp_get_device_opt() error: opt_type:%d is invalid!\n", opt_type);
return -1;
}
return ret;
}
#if 0
void sapp_statsd_flush_buf(void)
{
struct sockaddr_in statsd_addr;
if(sapp_global_single.statsd_fd < 0){
return;
}
if(sapp_global_single.stats_send_ptr - sapp_global_single.stats_send_buf > 0){
statsd_addr.sin_family = AF_INET;
statsd_addr.sin_addr.s_addr = sapp_global_single.statsd_ip_net;
statsd_addr.sin_port = sapp_global_single.statsd_port_net;
sendto(sapp_global_single.statsd_fd,
sapp_global_single.stats_send_buf,
sapp_global_single.stats_send_ptr - sapp_global_single.stats_send_buf,
MSG_DONTWAIT,
(struct sockaddr *)&statsd_addr,
sizeof(statsd_addr));
sapp_global_single.stats_send_ptr = sapp_global_single.stats_send_buf;
}
}
/*
Ϊ�˱���ÿ����Ϣ������һ��UDP��, �˴��Ȼ���, ����MTU�ٷ���.
���һ����Ϣ, ��Ҫ����sapp_statsd_flush_buf()ǿ��ˢ��һ��.
*/
void sapp_statsd_add_para(const char *key, unsigned long long value)
{
int ret;
int line_len;
char tmp_buf[128];
if(sapp_global_single.statsd_fd < 0){
return;
}
line_len = snprintf(tmp_buf, 128, "[sapp]%s:%lu|c\n", key, value);
if(sapp_global_single.stats_send_ptr - sapp_global_single.stats_send_buf + line_len > STATSD_SEND_MSS){
sapp_statsd_flush_buf();
}
sprintf(sapp_global_single.stats_send_ptr, "%s", tmp_buf);
sapp_global_single.stats_send_ptr += line_len;
}
#endif
void sapp_fs2_update_count(int field_index, unsigned long long value)
{
if(sapp_global_val->individual_fixed.field_stat2_para.field_stat2_handle != NULL){
FS_operate(sapp_global_val->individual_fixed.field_stat2_para.field_stat2_handle,
sapp_global_val->individual_fixed.field_stat2_para.fs_id_count_array[field_index],
0, FS_OP_SET, (long long)value);
}
}
void sapp_fs2_update_length(int field_index, unsigned long long value)
{
if(sapp_global_val->individual_fixed.field_stat2_para.field_stat2_handle != NULL){
FS_operate(sapp_global_val->individual_fixed.field_stat2_para.field_stat2_handle,
sapp_global_val->individual_fixed.field_stat2_para.fs_id_length_array[field_index],
0, FS_OP_SET, (long long)value);
}
}
void sapp_fs2_set_latency(int thead_seq, long long time_cost)
{
if(sapp_global_val->individual_fixed.field_stat2_para.field_stat2_handle != NULL){
FS_operate(sapp_global_val->individual_fixed.field_stat2_para.field_stat2_handle,
sapp_global_val->individual_fixed.field_stat2_para.fs_latency_id_array[thead_seq],
0, FS_OP_SET, time_cost);
}
}
int sapp_assistant_init(void)
{
sapp_fs2_init();
//sapp_line_protocol_init();
sapp_identify_broad_multicast_init();
pthread_mutex_init(&g_plug_Independent_thread_mutex, NULL);
if(sapp_global_val->config.cpu.send_only_threads_max_num > 0){
g_plug_Independent_thread_pid = (pthread_t *)calloc(1, sizeof(pthread_t));
}
return 1;
}
#ifdef __cplusplus
}
#endif
/************************ C++ compiler **************************************/
static int sapp_fs2_init(void)
{
char cfg_ip_str[32];
char fs_name[1024];
int cfg_port;
int fs2_opt;
int cycle;
int send_historgram;
void *fs2_handle;
unsigned short fs_server_port;
sapp_gval_individual_fixed_fs_t *pfs_para = &sapp_global_val->individual_fixed.field_stat2_para;
int fs2_local_enabled = 0;
int fs2_remote_enabled = 0;
if(sapp_global_val->config.profiling.log.remote.enabled){
if(strncasecmp(sapp_global_val->config.profiling.log.remote.remote_send_out_type, "field_stat2", strlen("field_stat2")) == 0){
fs2_remote_enabled = 1;
}else{
sapp_log(RLOG_LV_INFO, 10, 10, "profiling.log.remote.enabled, remote_send_out_type is line_protocol, field_stat2 send metrics to server is disable.\n");
fs2_remote_enabled = 0;
}
}else{
fs2_remote_enabled = 0;
}
fs2_local_enabled = sapp_global_val->config.profiling.log.local.enabled;
sapp_global_val->individual_fixed.field_stat2_para.field_stat2_handle = FS_create_handle();
if(NULL == sapp_global_val->individual_fixed.field_stat2_para.field_stat2_handle){
sapp_log(RLOG_LV_FATAL, 30, 30, "FS_create_handle() error: %s!\n", strerror(errno));
return -1;
}
fs2_handle = sapp_global_val->individual_fixed.field_stat2_para.field_stat2_handle;
if(fs2_remote_enabled){
sapp_global_val->individual_fixed.profiling_log_remote_handle = fs2_handle;
}
FS_set_para(fs2_handle, STAT_CYCLE, &sapp_global_val->config.profiling.log.interval, sizeof(int));
if(sapp_global_val->config.profiling.log.local.file_truncate_enabled){
fs2_opt = 1; /* 1:Rewrite ,2: Append. */
}else{
fs2_opt = 2; /* 1:Rewrite ,2: Append. */
}
FS_set_para(fs2_handle, PRINT_MODE, &fs2_opt, sizeof(int));
fs2_opt = 1;
FS_set_para(fs2_handle, PRINT_TRIGGER, &fs2_opt, sizeof(int));
fs2_opt = 1;
FS_set_para(fs2_handle, NOT_SEND_METRIC_TO_SERVER, &fs2_opt, sizeof(int));
if(fs2_local_enabled){
FS_set_para(fs2_handle, OUTPUT_DEVICE, sapp_global_val->config.profiling.log.local.log_file_name, strlen(sapp_global_val->config.profiling.log.local.log_file_name)+1);
}else{
sapp_log(RLOG_LV_INFO, 10, 10, "profiling.log.local.enabled is 0, not save local stat log file.\n");
FS_set_para(fs2_handle, OUTPUT_DEVICE, "/dev/null", strlen("/dev/null") + 1);
}
FS_set_para(fs2_handle, APP_NAME, sapp_global_val->config.profiling.log.remote.field_stat2.app_name, strlen(sapp_global_val->config.profiling.log.remote.field_stat2.app_name)+1);
if(fs2_remote_enabled){
FS_set_para(fs2_handle, STATS_SERVER_IP, (void *)sapp_global_val->config.profiling.log.remote.server_ip, strlen(sapp_global_val->config.profiling.log.remote.server_ip)+1);
fs_server_port = (unsigned short)sapp_global_val->config.profiling.log.remote.server_port;
FS_set_para(fs2_handle, STATS_SERVER_PORT, &fs_server_port, sizeof(short));
}
FS_set_para(fs2_handle, METRIS_FORMAT, &sapp_global_val->config.profiling.log.remote.field_stat2.metrics_type, sizeof(int));
pfs_para->fs_id_count_array[SAPP_STAT_RCV_ETHERNET] = FS_register(fs2_handle, FS_STYLE_FIELD, FS_CALC_CURRENT, "Eth_Pkt");
pfs_para->fs_id_length_array[SAPP_STAT_RCV_ETHERNET] = FS_register(fs2_handle, FS_STYLE_FIELD, FS_CALC_CURRENT, "Eth_Bit");
if((DEPOLYMENT_MODE_INLINE == sapp_global_val->config.packet_io.depolyment_mode_bin)
|| (DEPOLYMENT_MODE_TRANSPARENT == sapp_global_val->config.packet_io.depolyment_mode_bin)){
pfs_para->fs_id_count_array[SAPP_STAT_ETH_INBOUND] = FS_register(fs2_handle, FS_STYLE_FIELD, FS_CALC_CURRENT, "Pkt_Inbound");
pfs_para->fs_id_length_array[SAPP_STAT_ETH_INBOUND] = FS_register(fs2_handle, FS_STYLE_FIELD, FS_CALC_CURRENT, "Bit_Inbound");
pfs_para->fs_id_count_array[SAPP_STAT_ETH_OUTBOUND] = FS_register(fs2_handle, FS_STYLE_FIELD, FS_CALC_CURRENT, "Pkt_Outbound");
pfs_para->fs_id_length_array[SAPP_STAT_ETH_OUTBOUND] = FS_register(fs2_handle, FS_STYLE_FIELD, FS_CALC_CURRENT, "Bit_Outbound");
}
pfs_para->fs_id_count_array[SAPP_STAT_RCV_IPV4] = FS_register(fs2_handle, FS_STYLE_FIELD, FS_CALC_CURRENT, "Ipv4_Pkt");
pfs_para->fs_id_length_array[SAPP_STAT_RCV_IPV4] = FS_register(fs2_handle, FS_STYLE_FIELD, FS_CALC_CURRENT, "Ipv4_Bit");
pfs_para->fs_id_count_array[SAPP_STAT_RCV_IPV6] = FS_register(fs2_handle, FS_STYLE_FIELD, FS_CALC_CURRENT, "Ipv6_Pkt");
pfs_para->fs_id_length_array[SAPP_STAT_RCV_IPV6] = FS_register(fs2_handle, FS_STYLE_FIELD, FS_CALC_CURRENT, "Ipv6_Bit");
pfs_para->fs_id_count_array[SAPP_STAT_RCV_TCP] = FS_register(fs2_handle, FS_STYLE_FIELD, FS_CALC_CURRENT, "Tcp_Pkt");
pfs_para->fs_id_length_array[SAPP_STAT_RCV_TCP] = FS_register(fs2_handle, FS_STYLE_FIELD, FS_CALC_CURRENT, "Tcp_Bit");
pfs_para->fs_id_count_array[SAPP_STAT_RCV_UDP] = FS_register(fs2_handle, FS_STYLE_FIELD, FS_CALC_CURRENT, "Udp_Pkt");
pfs_para->fs_id_length_array[SAPP_STAT_RCV_UDP] = FS_register(fs2_handle, FS_STYLE_FIELD, FS_CALC_CURRENT, "Udp_Bit");
pfs_para->fs_id_count_array[SAPP_STAT_RCV_UNKNOWN] = FS_register(fs2_handle, FS_STYLE_FIELD, FS_CALC_CURRENT, "Unknown_Pkt");
pfs_para->fs_id_count_array[SAPP_STAT_RCV_UNKNOWN] = FS_register(fs2_handle, FS_STYLE_FIELD, FS_CALC_CURRENT, "Unknown_Bit");
pfs_para->fs_id_count_array[SAPP_STAT_TCP_STREAM_NEW] = FS_register(fs2_handle, FS_STYLE_FIELD, FS_CALC_CURRENT, "Tcp_Link_New");
pfs_para->fs_id_count_array[SAPP_STAT_TCP_STREAM_DEL] = FS_register(fs2_handle, FS_STYLE_FIELD, FS_CALC_CURRENT, "Tcp_Link_Del");
pfs_para->fs_id_count_array[SAPP_STAT_TCP_STREAM_DATA] = FS_register(fs2_handle, FS_STYLE_STATUS, FS_CALC_CURRENT, "Tcp_Concurrent");
pfs_para->fs_id_count_array[SAPP_STAT_TCP_STREAM_DOUBLE] = FS_register(fs2_handle, FS_STYLE_STATUS,FS_CALC_CURRENT, "Tcp_Link_Double");
pfs_para->fs_id_count_array[SAPP_STAT_TCP_STREAM_C2S] = FS_register(fs2_handle,FS_STYLE_STATUS, FS_CALC_CURRENT, "Tcp_Link_C2S");
pfs_para->fs_id_count_array[SAPP_STAT_TCP_STREAM_S2C] = FS_register(fs2_handle,FS_STYLE_STATUS, FS_CALC_CURRENT, "Tcp_Link_S2C");
pfs_para->fs_id_count_array[SAPP_STAT_SND_TCP_RST] = FS_register(fs2_handle, FS_STYLE_FIELD, FS_CALC_CURRENT, "Send_Tcp_Rst");
pfs_para->fs_id_count_array[SAPP_STAT_SND_TCP_SYNACK] = FS_register(fs2_handle, FS_STYLE_FIELD, FS_CALC_CURRENT, "Send_Tcp_S/A");
pfs_para->fs_id_count_array[SAPP_STAT_SND_UDP] = FS_register(fs2_handle, FS_STYLE_FIELD, FS_CALC_CURRENT, "Send_Udp");
if(unlikely(g_timestamp_record_sw))
{
for(int i = 0; i < g_packet_io_thread_num; i++)
{
char histogram_name[16];
sprintf(histogram_name, "tid_%d(ns)", i);
pfs_para->fs_latency_id_array[i] = FS_register_histogram(fs2_handle, //Field Stat���
FS_CALC_SPEED, //����ۼ�ֵ��˲ʱֵ
histogram_name, //ͳ�������ƣ��ַ���
1, //���ٵ���Сֵ
1000000, //���ٵ����ֵ
2); //���ȣ���С�����λ����Χ1~4
if(send_historgram == 0)
{
FS_set_para(fs2_handle, NOT_SEND_METRIC_TO_SERVER, &pfs_para->fs_latency_id_array[i], sizeof(int));
}
}
}
FS_start(fs2_handle);
return 0;
}
static int sapp_line_protocol_init(void)
{
log_line_protocol_handle_t *lp_handle;
if(0 == sapp_global_val->config.profiling.log.remote.enabled){
sapp_log(RLOG_LV_INFO, 10, 10, "profiling.log.remote.enabled = 0, line protocol is disable.\n");
return 0;
}
if(strncasecmp(sapp_global_val->config.profiling.log.remote.remote_send_out_type, "line_protocol", strlen("line_protocol")) != 0){
sapp_log(RLOG_LV_INFO, 10, 10, "profiling.log.remote.enabled, remote_send_out_type is field_stat2, line protocol is disable.\n");
return 0;
}
lp_handle = (log_line_protocol_handle_t *)calloc(1, sizeof(log_line_protocol_handle_t));
lp_handle->sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
lp_handle->sock_addr.sin_family = AF_INET;
inet_pton(AF_INET, sapp_global_val->config.profiling.log.remote.server_ip, &lp_handle->sock_addr.sin_addr.s_addr);
lp_handle->sock_addr.sin_port = ntohs((unsigned short)sapp_global_val->config.profiling.log.remote.server_port);
lp_handle->last_send_metrics_time = 0;
sapp_global_val->individual_fixed.profiling_log_remote_handle = lp_handle;
return 0;
}
|