Chinaunix首页 | 论坛 | 博客
  • 博客访问: 198325
  • 博文数量: 56
  • 博客积分: 125
  • 博客等级: 入伍新兵
  • 技术积分: 427
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-21 20:45
文章分类

全部博文(56)

文章存档

2018年(9)

2017年(10)

2016年(33)

2012年(4)

我的朋友

分类: LINUX

2017-04-19 13:54:25


点击(此处)折叠或打开

  1. #define _GNU_SOURCE
  2. #include <sched.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <stdio.h>
  6. #include <assert.h>
  7. #include <pthread.h>
  8. #include <string.h>
  9. #include <stdio.h>
  10. //#include "sqlite3.h"
  11. #include <sqlite3.h>
  12. #include <stdlib.h>
  13. #include <pthread.h>
  14. #include <string.h>
  15. #include<sys/time.h>
  16. #include<unistd.h>
  17. #include <netinet/in.h>
  18. #include <arpa/inet.h>
  19. extern int select_callback(void * data, int col_count, char ** col_values, char ** col_Name);


  20. struct _thread_tmp{
  21.         pthread_t thread;
  22.         int cpu;
  23. };

  24. sqlite3 *db;
  25. int g_count = 250000;
  26.     
  27. void *thread_func(void *param)
  28. {
  29.     int cpu = *(int *)param;
  30.     cpu_set_t mask;
  31.     cpu_set_t get;
  32.     int num = sysconf(_SC_NPROCESSORS_CONF);
  33.     int j;
  34.     int cpu_real = 0;

  35.     CPU_ZERO(&mask);
  36.     CPU_SET(cpu, &mask);

  37.     if (pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) < 0) {
  38.         perror("pthread_setaffinity_np");
  39.     }
  40.     CPU_ZERO(&get);
  41.     if (pthread_getaffinity_np(pthread_self(), sizeof(get), &get) < 0) {
  42.         fprintf(stderr, "get thread affinity failed\n");
  43.     }
  44.     for (j = 0; j < num; j++) {
  45.         if (CPU_ISSET(j, &get)) {
  46.             cpu_real = j;
  47.             //printf("thread %d is running in processor %d\n", (int)pthread_self(), j);
  48.         }
  49.     }
  50.     
  51.     
  52.     char sql[1024];
  53.     struct timeval start;
  54.     struct timeval end;
  55.     int i;
  56.     int result = 0;
  57.     char *ErrMsg = NULL;
  58.     memset(sql, 0, sizeof(sql));
  59.     gettimeofday(&start,NULL);
  60.     for(i = cpu; i < g_count; i = i+3){
  61. #ifdef STRING
  62.         char IPdotdec[20];
  63.         struct in_addr s;
  64.         s.s_addr = i;
  65.         inet_ntop(AF_INET,(void *)&s,IPdotdec,16);
  66.         snprintf(sql,sizeof(sql),"insert into ct values('%s',%d,'%s',%d)", IPdotdec,i,IPdotdec,i);
  67. #else
  68.         snprintf(sql,sizeof(sql),"insert into ct values(%d,%d,%d,%d)", i, i, i,i);
  69.         //snprintf(sql,sizeof(sql),"insert into ct values(%d)", i);
  70. #endif
  71.         result = sqlite3_exec(db,sql,NULL,NULL,&ErrMsg);
  72.         if(result != SQLITE_OK && SQLITE_BUSY) {
  73.             printf(" err %s\n",sql);
  74.             sqlite3_free(&ErrMsg);
  75.             sqlite3_close(db);
  76.             pthread_exit(NULL);
  77.         }
  78.     }

  79.     gettimeofday(&end,NULL);
  80.     int diff = 1000000 * (end.tv_sec-start.tv_sec)+ end.tv_usec-start.tv_usec;
  81.     printf("thread%d thedifference is %d\n",cpu_real, diff);
  82.     
  83.     pthread_exit(NULL);

  84. }


  85. int main(int argc, char *argv[])
  86. {
  87.     char *ErrMsg = NULL;
  88.     int result = 0;
  89.     char sql[1024];
  90.     //char *path = "./lwhtest.db";
  91.     char *path = NULL;
  92.     struct timeval start;
  93.     struct timeval end;
  94.     int i;
  95.     cpu_set_t mask;
  96.     CPU_ZERO(&mask);
  97.     CPU_SET(0, &mask);
  98.     if (sched_setaffinity(0, sizeof(mask), &mask) < 0) {
  99.         perror("sched_setaffinity");
  100.     }

  101. #ifdef SIGNAL
  102.     sqlite3_config(SQLITE_CONFIG_SINGLETHREAD);
  103.     printf("signal\n");
  104. #else
  105.     printf("Serialized\n");
  106. #endif    

  107.     result = sqlite3_open(path,&db);
  108.     if (result != SQLITE_OK) {
  109.         return -1;
  110.     }
  111.     //sqlite3_exec(db, "PRAGMA synchronous = OFF", 0,0,0);
  112. #ifdef NJOURNAL
  113.     sqlite3_exec(db, "PRAGMA journal_mode = OFF", 0, 0 ,0);
  114.     printf("journal off\n");
  115. #else
  116.     printf("journal on\n");
  117. #endif
  118. #ifdef STRING
  119.     snprintf(sql,sizeof(sql),"create table if not exists ct(sip text, sport int, dip text, dport int,primary key(sip))");    /*string*/
  120.     printf("string\n");
  121. #else
  122.     snprintf(sql,sizeof(sql),"create table if not exists ct(sip int, sport int, dip int, dport int,primary key(sip))"); /* int */
  123.     //snprintf(sql,sizeof(sql),"create table if not exists ct(sip int, sport int, dip int, dport int)");
  124.     //snprintf(sql,sizeof(sql),"create table if not exists ct(sip int)");
  125.     //snprintf(sql,sizeof(sql),"create table if not exists ct(sip int,primary key(sip))");
  126.     printf("no string\n");
  127. #endif
  128.     result = sqlite3_exec(db,sql,NULL,NULL,&ErrMsg);
  129.     if(result != SQLITE_OK && SQLITE_BUSY) {
  130.         printf(" err %s\n",sql);
  131.         sqlite3_free(ErrMsg);
  132.         sqlite3_close(db);
  133.         return -1;
  134.     }
  135.     gettimeofday(&start,NULL);
  136. #ifdef THREAD
  137.     struct _thread_tmp t[3];
  138.         
  139.     for(i =0; i < 3;i++){
  140.         t[i].cpu = i+1;
  141.         if (pthread_create(&t[i].thread, NULL, thread_func, &t[i].cpu) != 0) {
  142.             perror("pthread_create");
  143.         }
  144.     }

  145.     for(i =0; i < 3;i++){
  146.         pthread_join(t[i].thread, NULL);
  147.     }
  148. #else
  149.     for(i = 0; i < g_count; i++){
  150. #ifdef STRING
  151.         char IPdotdec[20];
  152.         struct in_addr s;
  153.         s.s_addr = i;
  154.         inet_ntop(AF_INET,(void *)&s,IPdotdec,16);
  155.         snprintf(sql,sizeof(sql),"insert into ct values('%s',%d,'%s',%d)", IPdotdec,i,IPdotdec,i);
  156. #else
  157.         snprintf(sql,sizeof(sql),"insert into ct values(%d,%d,%d,%d)", i,i,i,i);
  158. #endif
  159.         //snprintf(sql,sizeof(sql),"insert into ct values(%d)", i);
  160.         result = sqlite3_exec(db,sql,NULL,NULL,&ErrMsg);
  161.         if(result != SQLITE_OK && SQLITE_BUSY) {
  162.             printf(" err %s\n",sql);
  163.             sqlite3_free(ErrMsg);
  164.             sqlite3_close(db);
  165.             return -1;
  166.         }
  167.     }
  168. #endif    
  169.     gettimeofday(&end,NULL);
  170.     int diff = 1000000 * (end.tv_sec-start.tv_sec)+ end.tv_usec-start.tv_usec;
  171.     printf("thedifference is %d\n",diff);
  172.     
  173.     memset(sql, 0, sizeof(sql));
  174.     snprintf(sql,sizeof(sql),"select count(*) from ct");
  175.     result = sqlite3_exec(db,sql,select_callback,NULL,&ErrMsg);
  176.     if(result != SQLITE_OK) {
  177.         sqlite3_free(ErrMsg);
  178.         sqlite3_close(db);
  179.         return -1;
  180.     }

  181.     memset(sql, 0, sizeof(sql));
  182.     snprintf(sql,sizeof(sql),"delete from ct");
  183.     result = sqlite3_exec(db,sql,NULL,NULL,&ErrMsg);
  184.     if(result != SQLITE_OK) {
  185.         sqlite3_free(ErrMsg);
  186.         sqlite3_close(db);
  187.         return -1;
  188.     }

  189.     sqlite3_close(db);
  190.     
  191.     pthread_exit(NULL);

  192. }

  193. int select_callback(void * data, int col_count, char ** col_values, char ** col_Name)
  194. {
  195.   // 每条记录回调一次该函数,有多少条就回调多少次
  196.   int i;
  197.   for( i=0; i < col_count; i++){
  198.     printf( "count %d\n", atoi(col_values[i]));
  199.   }

  200.   return 0;
  201. }
保存到文件sqlite_memory.c
使用通用makefile
make clean;make string=1 thread=1 signal=1 njournal=1;./sqlite_memory
阅读(2796) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~