Colderleo's blog Colderleo's blog
首页
Linux
C++
Python
前端
工具软件
mysql
索引
关于
GitHub (opens new window)

Colder Leo

热爱代码的打工人
首页
Linux
C++
Python
前端
工具软件
mysql
索引
关于
GitHub (opens new window)
  • bug定位的一些情形
  • c++性能调优,可能的情况
  • total-编程知识点集锦
  • hpc_common.hpp
    • hpc_common.hpp
  • memory order 内存模型
  • 类型推导之auto-template-decltype
  • 完美转发forward源码分析
  • 左值和右值,右值引用、重载 std-move,引用折叠
  • cmake用法
  • alignas、alignof、sizeof实现内存对齐分配
  • 通过宏定义控制debug打印
  • 程序耗时性能测试
  • 线程池开源项目阅读
  • C++类中包含没有默认构造函数的成员
  • C++可变参数模板
  • C++属性继承 public protected private
  • C++智能指针
  • C++导出so的方法,以及extern C 注意事项
  • 四种spin lock
  • condition_variable和unique_lock
  • dpdk、kernel bypass
  • 智能网卡solarflare、Mellanox、X10等
  • 汇编寄存器和常见指令
  • c++ 类的静态成员变量未定义
  • C++获取类成员函数地址
  • preload示例
  • C++异常安全和RAII
  • C++11单例模式
  • C++歪门邪道
  • boost-program-option用法
  • c++17通过模板获取类成员的个数
  • 通过模板实现结构体成员拷贝-按成员名匹配
  • STL学习路径
  • boost库安装使用方法
  • C++文件读写
  • linux下socket通信demo,server和client
  • makefile写法
  • RxCpp
  • C++
gaoliu
2022-03-14
目录

hpc_common.hpp

# hpc_common.hpp

里面有一些好用的函数。

#pragma once
#include <new>
#include <iostream>
#include <vector>
#include <stdint.h>
#include <stddef.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <ftw.h>
#include <sys/mman.h>
#include <sys/mman.h>
#include <pthread.h>
#include <sched.h>
#include <signal.h>






namespace hpc_common {



/*********************** simple debug_print ********************************/
/*
#define DEBUG_LEVEL_INFO

#ifndef debug_info
#   if defined(DEBUG_LEVEL_INFO)
#       define debug_info(...)  do { printf("[line%d:%s] debug_info: ", __LINE__, __func__);  printf(__VA_ARGS__); printf("\n");} while(0)
#   else
#       define debug_info(...)  ((void) 0)
#   endif
#endif
*/



/*********************** debug_print ********************************/
#define DEBUG_LEVEL_INFO
#define DEBUG_LEVEL_WARNING
#define DEBUG_LEVEL_ERROR

#define DEBUG_COLOR_PRINT

#ifdef DEBUG_COLOR_PRINT
    #define DEBUG_COLOR_RED     "\033[31m"
    #define DEBUG_COLOR_GREEN   "\033[32m"
    #define DEBUG_COLOR_YELLOW  "\033[33m"
    #define DEBUG_COLOR_BLUE    "\033[34m"
    #define DEBUG_COLOR_PURPLE  "\033[35m"
    #define DEBUG_COLOR_RESET   "\033[m"
#else
    #define DEBUG_COLOR_RED 
    #define DEBUG_COLOR_GREEN 
    #define DEBUG_COLOR_YELLOW 
    #define DEBUG_COLOR_BLUE 
    #define DEBUG_COLOR_PURPLE 
    #define DEBUG_COLOR_RESET 
#endif



#ifndef filename_strip_path
#define filename_strip_path(x) strrchr(x,'/')?strrchr(x,'/')+1:x
#endif

#ifndef debug_prefix
#define debug_prefix(tag,color)  printf(color "[%s:%d:%s]: " tag DEBUG_COLOR_RESET, filename_strip_path(__FILE__), __LINE__, __func__)
#endif


#ifndef debug_info
#if  defined(DEBUG_LEVEL_INFO)
#  define debug_info(...)  do { debug_prefix("debug_info: ",     DEBUG_COLOR_GREEN);  printf(__VA_ARGS__); printf("\n");fflush(stdout);} while(0)
#else
#  define debug_info(...)  ((void) 0)
#endif
#endif


#ifndef debug_warning
#if defined(DEBUG_LEVEL_WARNING)
#  define debug_warning(...)    do { debug_prefix("debug_warning: ", DEBUG_COLOR_YELLOW); printf(__VA_ARGS__); printf("\n");fflush(stdout); } while(0)
#else
#  define debug_warning(...)    ((void) 0)
#endif
#endif


#ifndef debug_error
#if defined(DEBUG_LEVEL_ERROR)
#  define debug_error(...)    do { debug_prefix("debug_error: ",   DEBUG_COLOR_RED);    printf(__VA_ARGS__); printf("\n");fflush(stdout); } while(0)
#else
#  define debug_error(...)    ((void) 0)
#endif
#endif


#ifndef debug_assert
#if defined(DEBUG_LEVEL_ERROR)
#  define debug_assert(expr, ...)    do { if (!(expr)) { debug_prefix("debug_assert fail: ", DEBUG_COLOR_PURPLE); \
    printf(DEBUG_COLOR_PURPLE " [" #expr "] " DEBUG_COLOR_RESET); printf(__VA_ARGS__); printf("\n"); fflush(stdout); exit(-1); } } while(0)
#else
#  define debug_assert(...)     ((void) 0)
#endif
#endif




static inline void debug_print_16x_data(const uint8_t *data, int len) {
#ifdef DEBUG_LEVEL_INFO
    int i=0;
    while(1){
        for(int t=0; t<16; ++t) {
            if(i==len) {
                printf("\n");
                return;
            }
            printf("%02x ", *data);
            data++;
            i++;
        }
        printf("\n");
    }
#endif
}






/*********************** print ********************************/

#ifndef print_info
#define print_info(...)    do { debug_prefix("print_info: ", DEBUG_COLOR_GREEN); printf(__VA_ARGS__); printf("\n");} while(0)
#endif

#ifndef print_warning
#define print_warning(...)    do { debug_prefix("print_warning: ", DEBUG_COLOR_YELLOW); printf(__VA_ARGS__); printf("\n"); } while(0)
#endif

#ifndef print_error
#define print_error(...)    do { debug_prefix("print_error: ",   DEBUG_COLOR_RED);  \
    printf(__VA_ARGS__);  printf(" , errno=%d, errstr: %s\n",errno,strerror(errno)); exit(errno);} while(0)
#endif

#ifndef error_exit
#define error_exit(...)    do { debug_prefix("error_exit: ",   DEBUG_COLOR_RED);   \
    printf(__VA_ARGS__);  printf(" , errno=%d, errstr: %s\n",errno,strerror(errno)); exit(errno);} while(0)
#endif













/*********************** macro / bit ********************************/

#define _MACRO_TO_STR_AUX(macro) #macro
#define MACRO_TO_STR(macro) _MACRO_TO_STR_AUX(macro)


// align should be power of 2, like 1024
#define HPC_ROUND_UP(value, align)  (((value) + ((align)-1u)) & ~((align)-1u))
#define HPC_ROUND_DOWN(value, align)  ((value) & ~((align)-1u))
#define HPC_IS_POWER2(value)		((value) > 0 && (((value) & (~(value) + 1u)) == (value)))












/*********************** atomic ********************************/

#ifndef mem_barrier
#define mem_barrier() do { __asm__ __volatile__("" : : : "cc", "memory" ); } while(0)
#endif

#ifndef cpu_barrier
#define cpu_barrier() do { __asm__ __volatile__("lock" : addl $0 0(%%esp) : : "cc", "memory" ); } while(0)
#endif


#ifndef mem_read
#define mem_read(var) do { __asm__ __volatile__("" : "=m"(var) : :); } while(0)
#endif

#ifndef memory_write
#define memory_write(var) do { __asm__ __volatile__("" : : "m"(var) : ); } while(0)
#endif





















/*********************** string ********************************/

template<typename... Args>
std::string string_sprintf(const char* format, Args... args) {
    char buf[4096];
    std::snprintf(buf, 4096, format, args...);
    std::string str(buf);
    return std::move(str);
}
















/*********************** memory mmap ********************************/

//touch mm page to tlb
static inline void hpc_touch_pages(void *base, size_t len, size_t page_size)
{
    for (uint i = 0; i < len; i += page_size)   {
        volatile uint8_t &byte = *((volatile uint8_t *)base + i);
        __asm__ __volatile__("" : : "m"(byte) : );
    }
}

static inline void* hpc_mmap(int fd, size_t len)
{
    int flags = MAP_SHARED;
    // if (huge_page)  {
    //     // flags = MAP_SHARED | MAP_HUGETLB;
    // }
    auto ret = mmap(NULL, len, PROT_READ | PROT_WRITE, flags, fd, 0);
    close(fd);
    return ret;
}


static inline int hpc_unmap(void *addr, size_t len)
{
    if (NULL != addr) {
        return munmap(addr, len);
    }
    return 0;
}


/**
 * max msglen 1042;
 * usage:     
 *   try {
 *       int aa = 5;
 *       throw HpcException("some thing wrong: aa=%d", aa);
 *   }
 *   catch(HpcException e) {
 *       printf("catch exception: %s\n", e.what());
 *   }
**/
class HpcException: std::exception {
public:
    template<typename... Args>
    explicit HpcException(const char* format, Args... args) throw() {
        constexpr int msglen = 4096;
        char buf[msglen];
        std::snprintf(buf, msglen, format, args...);
        what_.assign(buf);
    }
    const char* what() const throw()
    {
        return what_.c_str();
    }
private:
    std::string what_;
};



// is_new will be set true if the file is new created, else false.
// return map addr.
static inline void * hpc_map_new_or_exist(const char *path, size_t len, bool &is_new)
{
    is_new = true;
    int fd = open(path, (O_CREAT | O_EXCL | O_RDWR), 0700);
    if (fd<0) {
        is_new = false;
    }

    fd = open(path, (O_CREAT | O_RDWR), 0700);
    if (fd<0)  throw shm_bad_alloc("file open error, path=%s, len=%lu, lasterr=%s", path, len, strerror(errno));

    if (ftruncate(fd, (off_t)len) < 0) {
        throw shm_bad_alloc("file ftruncate error, path=%s, len=%lu, lasterr=%s", path, len, strerror(errno));
    }
    
    auto ret = hpc_mmap(fd, len);
    if (ret == MAP_FAILED) {
        throw shm_bad_alloc("file mmap error, path=%s, len=%lu, lasterr=%s", path, len, strerror(errno));
    }
    if (is_new)  memset(ret, 0, len);
    else hpc_touch_pages(ret, len, 4096);
    return ret;
}


// open or create file, and reset to 0.
static inline void * hpc_map_new(const char *path, size_t len)
{
    //create, rewrite.
    int fd = open(path, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
    // debug_info("fd = %d\n", fd);
    if (fd<0)  throw shm_bad_alloc("file open error, path=%s, len=%lu", path, len);

    if (ftruncate(fd, (off_t)len) < 0)
        throw shm_bad_alloc("file ftruncate error, path=%s, len=%lu", path, len);
    
    auto ret = hpc_mmap(fd, len);
    if (ret == MAP_FAILED)
        throw shm_bad_alloc("mmap error, path=%s, len=%lu, lasterr=%s", path, len, strerror(errno));

    memset(ret, 0, len);
    // hpc_touch_pages(ret, len, 4096);
    return ret;
}

static inline void * hpc_map_exist(const char *path, size_t len)
{
	// debug_info("path=%s\n", path);
    
    int fd = open(path, O_RDWR);
    if(fd<0) 
        throw shm_bad_alloc("open file fail: %s", path);
    
    struct stat sb;
    if (fstat(fd, &sb) != 0)
        throw shm_bad_alloc("get fstat fail: fd=%d, file=%s", fd, path);

    auto ret_addr = hpc_mmap(fd, len);
    if (ret_addr == MAP_FAILED)
        throw shm_bad_alloc("mmap fail: file=%s, map_len=%lu, fd=%d", path, len, fd);
        
    hpc_touch_pages(ret_addr, len, 4096);
    return ret_addr;
}






















/*********************** time performance ********************************/

static inline uint64_t rdtsc() {
    uint32_t lo, hi;
    __asm__ volatile("rdtsc" : "=a"(lo), "=d"(hi) : :);
    return (uint64_t)hi<<32|lo;
    //return __builtin_ia32_rdtsc();
}

static inline int64_t get_nano_clock()
{
    struct timespec ts;
    if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0) {
        return -1;
    }

    return ((int64_t)ts.tv_sec * 1000000000) + ts.tv_nsec;
}


//int us may cause overflow bug.
void usleep_inwhile(int64_t us) {
    timespec ts;
    clock_gettime(CLOCK_REALTIME, &ts);
    int64_t start_ns = ts.tv_sec*1000000000+ts.tv_nsec;
    int64_t end_ns = start_ns + us*1000;
    while(1) {
        clock_gettime(CLOCK_REALTIME, &ts);
        if(ts.tv_sec*1000000000+ts.tv_nsec > end_ns) {
            break;
        }
    }
}














/*********************** system ********************************/

inline void set_thread_affinity(uint32_t cpu_id)
{

    if (cpu_id < 0)
        return;

    cpu_set_t cpuset;
    CPU_ZERO(&cpuset);
    CPU_SET(cpu_id, &cpuset);

    if(-1 == pthread_setaffinity_np(pthread_self(), sizeof(cpuset), &cpuset)) {
        print_error("set thread affinity fail, cpu_id=%u", cpu_id);
        return;
    }
}


inline void SetCpuAffinity(uint32_t cpu_id)
{
    if (cpu_id < 0)
        return;

    cpu_set_t cpuset;
    CPU_ZERO(&cpuset);
    CPU_SET(cpu_id, &cpuset);

    if(sched_setaffinity(0, sizeof(cpuset), &cpuset) == -1){
        print_error("set cpu affinity fail, cpu_id=%u", cpu_id);
        return;
    }
}


//popen print to stdout
static inline pid_t popen_tostd(const char *cmd){
    pid_t pid = fork();
    if(pid!=0) {
        return pid;
    }
    char _cmd[512]{0};
    sprintf(_cmd, "%s", cmd);
    execl("/bin/sh", "sh", "-c", _cmd, NULL);
    exit(0); //will never get here.
}

static inline void Signal(int signum, sighandler_t handler) {
    struct sigaction action, old_action;
    action.sa_handler = handler;
    sigemptyset(&action.sa_mask);
    sigaddset(&action.sa_mask, signum);
    action.sa_flags = SA_RESTART;
    
    if(sigaction(signum, &action, &old_action) < 0) {
        fprintf(stderr, "sigaction error, signum=%d, errno=%s", signum, strerror(errno));
    }
}



















/*********************** bits ********************************/

static inline constexpr bool is_power_of_2(size_t num) {
    return (num >= 2) && ((num & (num - 1)) == 0);
} 












































/*********************** list ********************************/

/**
--ring list: h--n1--n2--h

struct SS *g_ss_list = new SS;

list_head *p, *pnext;
list_for_each_safe(p, pnext, &g_ss_list) {
    if(p->xxx)
        list_del(p);
}

attention: do not add the same node twice to one list.
****/


#define init_list_head(ptr) do { \
	(ptr)->next = (ptr); (ptr)->prev = (ptr); \
} while (0)

#define list_entry(ptr, type, member) \
	((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))

#define list_for_each(pos, head) \
	for (pos = (head)->next; pos != (head); \
        	pos = pos->next)
#define list_for_each_prev(pos, head) \
	for (pos = (head)->prev; pos != (head); \
        	pos = pos->prev)

// can list_del(p) safely
#define list_for_each_safe(pos, n, head) \
	for (pos = (head)->next, n = pos->next; pos != (head); \
		pos = n, n = pos->next)


#define list_for_each_safe_reverse(pos, n, head) \
	for (pos = (head)->prev, n = pos->prev; pos != (head); \
		pos = n, n = pos->prev)


#define list_for_each_entry(pos, head, member)				\
	for (pos = list_entry((head)->next, typeof(*pos), member);	\
	     &pos->member != (head); 					\
	     pos = list_entry(pos->member.next, typeof(*pos), member))

#define list_for_each_entry_safe(pos, n, head, member)			\
	for (pos = list_entry((head)->next, typeof(*pos), member),	\
		n = list_entry(pos->member.next, typeof(*pos), member);	\
	     &pos->member != (head); 					\
	     pos = n, n = list_entry(n->member.next, typeof(*n), member))


template<typename list_head>
static inline void __list_add(list_head *newl, list_head *prev, list_head *next)
{
	newl->next = next;
	newl->prev = prev;
	next->prev = newl;
	prev->next = newl;
}


// add after:  h--n1--n2--h   =>     h--newl--n1--n2--h
template<typename list_head>
static inline void list_add(list_head *head, list_head *newl)
{
	__list_add(newl, head, head->next);
}


// add to tail: h--n1--n2--h   =>     h--n1--n2--newl--h
template<typename list_head>
static inline void list_add_tail(list_head *head, list_head *newl)
{
	__list_add(newl, head->prev, head);
}


// same as list_add_tail
template<typename list_head>
static inline void list_add_before(list_head *cur, list_head *newl)
{
	__list_add(newl, cur->prev, cur);
}


template<typename list_head>
static inline void __list_del(list_head *prev, list_head *next)
{
	next->prev = prev;
	prev->next = next;
}


//remove from list
template<typename list_head>
static inline void list_del(list_head *entry)
{
    // list_head->inuse = false;
	__list_del(entry->prev, entry->next);
	entry->next = NULL;
	entry->prev = (list_head *) 0;
}


template<typename list_head>
static inline void list_del_init(list_head *entry)
{
	__list_del(entry->prev, entry->next);
	INIT_LIST_HEAD(entry); 
}


template<typename list_head>
static inline void list_move(list_head *list, list_head *head)
{
    __list_del(list->prev, list->next);
    list_add(list, head);
}


template<typename list_head>
static inline void list_move_tail(list_head *list, list_head *head)
{
    __list_del(list->prev, list->next);
    list_add_tail(list, head);
}


template<typename list_head>
static inline int list_empty(list_head *head)
{
	return head->next == head;
}


template<typename list_head>
static inline void __list_splice(list_head *list, list_head *head)
{
	list_head *first = list->next;
	list_head *last = list->prev;
	list_head *at = head->next;

	first->prev = head;
	head->next = first;

	last->next = at;
	at->prev = last;
}


template<typename list_head>
static inline void list_splice(list_head *list, list_head *head)
{
	if (!list_empty(list)) {
		__list_splice(list, head);
	}
}


template<typename list_head>
static inline void list_splice_init(list_head *list, list_head *head)
{
	if (!list_empty(list)) {
		__list_splice(list, head);
		INIT_LIST_HEAD(list);
	}
}


// template<typename T>
// void __list_insert(T *neww, T *prev, T *next){
//     if(next)
//         next->prev = neww;

//     // neww should not be null
//     neww->next = next;
//     neww->prev = prev;

//     if(prev)
//         prev->next = neww;
// }

// template<typename T>
// void list_insert_after(T *cur, T *neww){
//     __list_insert(neww, cur, cur->next);
// }


// template<typename T>
// void list_insert_before(T *cur, T *neww){
//     __list_insert(neww, cur->prev, cur);
// }

// //do not change cur.
// template<typename T>
// void list_remove(T *cur){
//     if(cur->prev)
//         cur->prev->next = cur->next;
        
//     if(cur->next)
//         cur->next->prev = cur->prev;
// }













/*********************** file ********************************/

template<typename... Args>
static inline std::string get_first_exist_file(Args ...args)
{
    std::vector<std::string> filenames{args...};
    for(auto p = filenames.begin(); p != filenames.end(); ++p) {
        if(fopen(p->c_str(), "r"))
            return *p;
    }
    return std::string();
}



} //end of namespace hpc_common


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
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
编辑 (opens new window)
上次更新: 2023/05/07, 17:27:54
total-编程知识点集锦
memory order 内存模型

← total-编程知识点集锦 memory order 内存模型→

最近更新
01
通过模板实现结构体成员拷贝-按成员名匹配
05-07
02
c++17通过模板获取类成员的个数
05-01
03
avx-sse切换惩罚
04-30
更多文章>
Theme by Vdoing | Copyright © 2019-2023 Colder Leo | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×