/// \brief put work for heated chains on to the heated-chain-stack /// /// queue the work needed to be done into the stack of heated chains /// if there is an error the routien will abort with an /// error "filling heating queue failed" void fill_tpool (tpool_t tpool, world_fmt ** universe, int universe_size) { int i; for (i = 0; i < universe_size; i++) { if (!tpool_add_work (tpool, thread_tree_update, (void *) universe[i], i)) error ("Filling heating queue failed"); } }
int main(int argc, char **argv) { printf("--------- Server --------------\n"); int port = PORT; if (argc>1) port = atoi(argv[1]); //create thread pool if (tpool_create(THREAD_NUM) != 0) { printf("tpool_create failed\n"); exit(-1); } printf("--- Thread Pool Strat ---\n"); //initial server int listenfd = Server_init(port); socklen_t sockaddr_len = sizeof(struct sockaddr); //epoll static struct epoll_event ev, events[EPOLL_SIZE]; int epfd = epoll_create(EPOLL_SIZE); ev.events = EPOLLIN; ev.data.fd = listenfd; epoll_ctl(epfd, EPOLL_CTL_ADD, listenfd, &ev); while(1){ int events_count = epoll_wait(epfd, events, EPOLL_SIZE, -1); int i=0; //accept resquest for(; i<events_count; i++){ if(events[i].data.fd == listenfd) { int connfd; struct sockaddr_in clientaddr; while( ( connfd = accept(listenfd, (struct sockaddr *)&clientaddr, &sockaddr_len) ) > 0 ) { printf("EPOLL: Received New Connection Request---connfd= %d\n",connfd); struct args *p_args = (struct args *)malloc(sizeof(struct args)); p_args->fd = connfd; p_args->recv_finfo = recv_fileinfo; p_args->recv_fdata = recv_filedata; //add task to the work list tpool_add_work(worker, (void*)p_args); } } } } return 0; }
/** * Retrieve the blocks of the given file currently in the Linux Page Cache. * * @param str IN The string array. * @param max IN The number of elements in the array. * @param value IN The search value. * @returns index of the array where the value is located or -1 if not found. */ void PrimeCache (void) { sqlite3 *dbh = NULL; /* Database Handle */ sqlite3_stmt *sth = NULL; /* SQLite Statement Handle */ int rc = 0; /* SQLite Return Code */ printf("\nBeginning Cache Priming\n"); fflush(stdout); /* Enable the SQLite shared cache */ // sqlite3_enable_shared_cache(1); /* Open the SQLite database */ rc = sqlite3_open(dbFileName, &dbh); sqlite3_busy_handler(dbh, busyHandler, NULL); /* Prepare the SQL */ rc = sqlite3_prepare(dbh, SQL_CACHED_FILES_QUERY, -1, &sth, NULL); /* * Iterate over the block ranges, reading them into cache. */ while ((rc = sqlite3_step(sth)) != SQLITE_ERROR) { if (rc == SQLITE_ROW) { int fileId = sqlite3_column_int(sth, 0); if (POINTER_IS_VALID(thp)) tpool_add_work(thp, PrimeCacheForFile, (void *)fileId); else PrimeCacheForFile(fileId); } else if (rc == SQLITE_DONE) { break; } else if (rc == SQLITE_BUSY) { continue; } } sqlite3_finalize(sth); sqlite3_close(dbh); } /* PrimeCache() */
int main(void) { int i; tpool_t test_pool; tpool_init(&test_pool, 8, 20); for ( i = 0; i < 5; i++) { tpool_add_work(test_pool, job, str[i]); } tpool_destroy(test_pool, 1); return 0; }
/** * event_serial - Serial event trigger, this module get task from serial * interface(@intent) and insert into pending list when * the data has generated. * * @param[in] arg Resources of this module. * * @return value present condition at this module operation * */ void *event_serial(void *arg) { mngtask *mtask = (mngtask *)arg; serial_manage *serial = mtask->serial; smrt_event_op *op = &(serial->op); char data[DATA_SIZE] = {'\0'}; int len = -1; int ret = -1; int i; pending *pendnode = NULL; DEBUG("I am in event_serial! %d ^_^ ", serial->gIhandle); printf("serial_errno:%d\n", serial_errno); ret = op->evop_start(serial); if(ret < 0) //PDIE("|ERROR| Start <%s>", serial->gCsname); op->evop_stop(&serial->uCrunning); tpool_add_work(serial_write, serial); while(serial->uCrunning){ memset(data, 0, DATA_SIZE); len = op->evop_read(serial->gIhandle, data, 100); if(len > 0){ pendnode = (pending *)malloc(sizeof(pending)); if(pendnode){ mtask->pend.action = 't'; memcpy(pendnode->command, data, 100); //printf("-------%x %x %x %x %x %x %x %x %x-------\n", pendnode->command[0], pendnode->command[1], pendnode->command[2], pendnode->command[3], pendnode->command[4], pendnode->command[5], pendnode->command[6], pendnode->command[7], pendnode->command[8]); task_pending_add(&mtask->pend, pendnode); mtask->trigger(mtask->intent, mtask->pend.action); } else{ LOG("Trigger:allocate pending node failed."); continue; } } else{ op->evop_stop(&serial->uCrunning); } } DEBUG("Serial reader exit!"); }
static void sig_usr1( int signo ) { struct stat statbuf; work_info_t wi; conn_t *con; traceLog(LOG_NOTICE, "Received SIGUSR1"); stat( srv.rulefile, &statbuf ); if (statbuf.st_mtime != srv.mtime ) { srv.mtime = statbuf.st_mtime; con = conn_new(); con->srv = &srv; con->rs = srv.root; memset( &wi, 0, sizeof( work_info_t )); wi.conn = con; wi.routine = &do_reread_rulefile; reread_rulefile = 1; tpool_add_work( srv.work, &wi); } }
int main(int argc, char *argv[]) { tpool_t *pool; logmy=log_open("test.log", 0); pool=tpool_init(10,20,1); socket_branch(); char Construction[2]; while(1) { printf("Please Input Construction number On Server:\t"); scanf("%s", Construction); printf("%s\n", Construction); switch (Construction[1]) { case 9: tpool_destroy(pool,1); log_close(logmy); pthread_exit(NULL); exit (EXIT_SUCCESS); case 8: exit (EXIT_FAILURE); case 6: break; default: break; } struct sockaddr_in client_addr; int client_addr_length =sizeof(client_addr); int new_server_socket_fd =accept(server_socket_fd,(struct sockaddr*)&client_addr, &client_addr_length); if(new_server_socket_fd < 0) { perror("Server Accept Failed:"); break; } else { printf("Accept Client Socket Address\n"); char peeraddrstr[60]; char peerip[18]; time_t timep; time(&timep); int len = sizeof(client_addr); if(!getpeername(new_server_socket_fd, (struct sockaddr *)&client_addr, &len)) { sprintf(peeraddrstr, "time: %s\npeer address: %s:%d\n\n",ctime(&timep) , inet_ntop(AF_INET, &client_addr.sin_addr, peerip, sizeof(peerip)), ntohs(client_addr.sin_port)); printf("%s\n", peeraddrstr); } tpool_add_work(pool,thread,peeraddrstr); } } sleep(5); tpool_destroy(pool,1); log_close(logmy); pthread_exit(NULL); }
/** * Retrieve the blocks of the given file currently in the Linux Page Cache. * * @param str IN The string array. * @param max IN The number of elements in the array. * @param value IN The search value. * @returns index of the array where the value is located or -1 if not found. */ void BuildCacheProfile (void) { PGconn *pgh = NULL; /* Postgres Connection Handle */ PGresult *pgrsh = NULL; sqlite3 *dbh = NULL; /* SQLite Database Handle */ sqlite3_stmt *sth = NULL; /* SQLite Statement Handle */ int i = 0; /* Generic Iterator */ int rc = 0; /* SQLite Return Code */ int dbOid = 0; /* Postgres Database OID */ int dbTablespaceOid = 0; /* Postgres Default Tablespace OID */ char *baseDir = get_current_dir_name(); /* Open the SQLite database */ rc = sqlite3_open(dbFileName, &dbh); sqlite3_busy_handler(dbh, busyHandler, NULL); /* Connect to Postgres */ pgh = PQsetdbLogin(NULL, NULL, NULL, NULL, pgConnectString == NULL ? "postgres" : pgConnectString, NULL, pgPassword); if (PQstatus(pgh) == CONNECTION_BAD) { ERROR_PRINT("%s", "Connection Test Failed\n"); ERROR_PRINT("SQLERRMC: %s\n", PQerrorMessage(pgh)); PQfinish(pgh); exit(EXIT_FAILURE); } /* Retrieve the Postgres block size */ pgrsh = PQexec(pgh, PG_BLCKSZ_QUERY); if (PQresultStatus(pgrsh) == PGRES_TUPLES_OK) { db_block_size = atoi(PQgetvalue(pgrsh, 0, 0)); PQclear(pgrsh); /* Inform user of block sizes */ printf("PG BLCKSZ........ %u\n", db_block_size); printf("OS Page Size..... %u\n", (uint32_t)getpagesize()); } else { ERROR_PRINT("%s", "Could not retrieve database settings\n"); ERROR_PRINT("SQLERRMC: %s\n", PQerrorMessage(pgh)); PQclear(pgrsh); PQfinish(pgh); } /* Retrieve the Postgres block size */ pgrsh = PQexec(pgh, PG_DEFAULT_TABLESPACE_QUERY); if (PQresultStatus(pgrsh) == PGRES_TUPLES_OK) { dbOid = atoi(PQgetvalue(pgrsh, 0, 0)); dbTablespaceOid = atoi(PQgetvalue(pgrsh, 0, 1)); PQclear(pgrsh); } else { ERROR_PRINT("%s", "Could not retrieve database settings\n"); ERROR_PRINT("SQLERRMC: %s\n", PQerrorMessage(pgh)); PQclear(pgrsh); PQfinish(pgh); } /* Retrieve the Postgres tablespace information */ pgrsh = PQexec(pgh, PG_OBJECT_QUERY); if (PQresultStatus(pgrsh) == PGRES_TUPLES_OK) { int numRows = PQntuples(pgrsh); char filePath[1024]; printf("\nBeginning cache profiling for %d database objects.\n", numRows); for (i = 0; i < numRows; i++) { struct dirent *dp = NULL; /* Directory Entry Pointer */ DIR *dfd = NULL; /* Directory Stream */ char pattern1[64]; char pattern2[64]; char *name = PQgetvalue(pgrsh, i, 0); char *kind = PQgetvalue(pgrsh, i, 1); int tableSpace = atoi(PQgetvalue(pgrsh, i, 2)); int fileNode = atoi(PQgetvalue(pgrsh, i, 3)); snprintf(pattern1, sizeof(pattern1), "%d", fileNode); snprintf(pattern2, sizeof(pattern2), "%d.*", fileNode); if (tableSpace == 0 || tableSpace == dbTablespaceOid) snprintf(filePath, sizeof(filePath), "%s/pg_tblspc/%d/%d", pgDataDir, dbTablespaceOid, dbOid); dfd = opendir(filePath); if (dfd != NULL) { chdir(filePath); while ((dp = readdir(dfd)) != NULL) { struct stat sb; /* Attempt to parse the given PG log file */ if (stat(dp->d_name, &sb) == -1) continue; /* Make sure this is a normal file */ if (!S_ISREG(sb.st_mode)) continue; /* Does this file match our pattern */ DEBUG_PRINT("Checking pattern [%s] and [%s] for file [%s]\n", pattern1, pattern2, dp->d_name); if (fnmatch(pattern1, dp->d_name, 0) == 0 || fnmatch(pattern2, dp->d_name, 0) == 0) { char fullFilePath[1024]; snprintf(fullFilePath, sizeof(fullFilePath), "%s/%s", filePath, dp->d_name); if (POINTER_IS_VALID(thp)) tpool_add_work(thp, ProfileCacheForFile, (void *)strdup(fullFilePath)); else ProfileCacheForFile(strdup(fullFilePath)); } } /* We're done, close the directory stream */ closedir(dfd); chdir(baseDir); } } PQclear(pgrsh); } else { ERROR_PRINT("%s", "Could not retrieve database settings\n"); ERROR_PRINT("SQLERRMC: %s\n", PQerrorMessage(pgh)); PQclear(pgrsh); PQfinish(pgh); } /* We're done with the connection */ PQfinish(pgh); } /* BuildCacheProfile() */