void *cuckoo_thread(void *arg) { int i; int id = *((int *) (arg)); int tot_val_sum = 0; red_printf("Thread %d: Starting lookups\n", id); struct timespec start, end; while(1) { clock_gettime(CLOCK_REALTIME, &start); for(i = 0; i < NUM_KEYS; i += BATCH_SIZE) { tot_val_sum += process_batch(&keys[i]); } clock_gettime(CLOCK_REALTIME, &end); double seconds = (end.tv_sec - start.tv_sec) + (double) (end.tv_nsec - start.tv_nsec) / 1000000000; red_printf("Thread ID: %d, Rate = %.2f M/s. Value sum = %d\n", id, NUM_KEYS / (seconds * 1000000), tot_val_sum); } }
int main(int argc, char **argv) { int i; assert(argc == 2); int num_threads = atoi(argv[1]); assert(num_threads >= 1 && num_threads <= CUCKOO_MAX_THREADS); red_printf("main: Initializing shared cuckoo hash table\n"); cuckoo_init(&keys, &ht_index); /**< Thread structures */ pthread_t worker_threads[CUCKOO_MAX_THREADS]; for(i = 0; i < num_threads; i ++) { int tid = i; pthread_create(&worker_threads[i], NULL, cuckoo_thread, &tid); /**< Ensure that threads don't use the same keys close in time */ sleep(1); } for(i = 0; i < num_threads; i ++) { pthread_join(worker_threads[i], NULL); } /**< The work never ends */ assert(0); return 0; }
ZlibEncoder* zlib_encoder_create(ZlibEncoderUsrContext *usr, int level) { ZlibEncoder *enc; int z_ret; if (!usr->more_space || !usr->more_input) { return NULL; } enc = spice_new0(ZlibEncoder, 1); enc->usr = usr; enc->strm.zalloc = Z_NULL; enc->strm.zfree = Z_NULL; enc->strm.opaque = Z_NULL; z_ret = deflateInit(&enc->strm, level); enc->last_level = level; if (z_ret != Z_OK) { red_printf("zlib error"); free(enc); return NULL; } return enc; }
int main() { int i; int tid[NUM_THREADS]; pthread_t thread[NUM_THREADS]; /** < Ensure that locks are cacheline aligned */ assert(sizeof(lock_t) == 64); /** < Allocate the shared nodes */ red_printf("Allocting %d nodes\n", NUM_NODES); nodes = (node_t *) malloc(NUM_NODES * sizeof(node_t)); assert(nodes != NULL); for(i = 0; i < NUM_NODES; i ++) { nodes[i].a = rand(); nodes[i].b = nodes[i].a + 1; } /** < Allocate the striped spinlocks */ red_printf("Allocting %d locks\n", NUM_LOCKS); locks = (lock_t *) malloc(NUM_LOCKS * sizeof(lock_t)); assert(locks != NULL); for(i = 0; i < NUM_LOCKS; i++) { pthread_spin_init(&locks[i].lock, 0); } /** < Launch several reader threads and a writer thread */ for(i = 0; i < NUM_THREADS; i++) { tid[i] = i; if(i == -1) { red_printf("Launching writer thread with tid = %d\n", tid[i]); pthread_create(&thread[i], NULL, writer, &tid[i]); } else { red_printf("Launching reader thread with tid = %d\n", tid[i]); pthread_create(&thread[i], NULL, reader, &tid[i]); } } for(i = 0; i < NUM_THREADS; i++) { pthread_join(thread[i], NULL); } exit(0); }
void *reader( void *ptr) { struct timespec start, end; int tid = *((int *) ptr); uint64_t seed = 0xdeadbeef + tid; int sum = 0, i; /** < The node and lock to use in an iteration */ int node_id, lock_id; /** < Total number of iterations (for measurement) */ int num_iters = 0; clock_gettime(CLOCK_REALTIME, &start); while(1) { if(num_iters == ITERS_PER_MEASUREMENT) { clock_gettime(CLOCK_REALTIME, &end); double seconds = (end.tv_sec - start.tv_sec) + (double) (end.tv_nsec - start.tv_nsec) / GHZ_CPS; printf("Reader thread %d: rate = %.2f M/s. Sum = %d\n", tid, num_iters / (1000000 * seconds), sum); num_iters = 0; clock_gettime(CLOCK_REALTIME, &start); } node_id = fastrand(&seed) & NUM_NODES_; lock_id = node_id & NUM_LOCKS_; #if USE_SKIP == 1 while(pthread_spin_trylock(&locks[lock_id].lock) == EBUSY) { lock_id = (lock_id + 1) & NUM_LOCKS_; } #else pthread_spin_lock(&locks[lock_id].lock); /** < Critical section begin */ if(nodes[node_id].b != nodes[node_id].a + 1) { red_printf("Invariant violated\n"); } #endif for(i = 0; i < WRITER_COMPUTE; i ++) { sum += CityHash32((char *) &nodes[node_id].a, 4); sum += CityHash32((char *) &nodes[node_id].b, 4); } /** < Critical section end */ pthread_spin_unlock(&locks[lock_id].lock); num_iters ++; } }
void *writer( void *ptr) { struct timespec start, end; int tid = *((int *) ptr); uint64_t seed = 0xdeadbeef + tid; int sum = 0; /** < The node and lock to use in an iteration */ int i, j, node_id, lock_id; /** < Total number of iterations (for measurement) */ int num_iters = 0; clock_gettime(CLOCK_REALTIME, &start); while(1) { if(num_iters == ITERS_PER_MEASUREMENT) { clock_gettime(CLOCK_REALTIME, &end); double seconds = (end.tv_sec - start.tv_sec) + (double) (end.tv_nsec - start.tv_nsec) / GHZ_CPS; node_id = fastrand(&seed) & NUM_NODES_; red_printf("Writer thread %d: rate = %.2f M/s. " "Random node: (%lld, %lld)\n", tid, num_iters / (1000000 * seconds), nodes[node_id].a, nodes[node_id].b); num_iters = 0; clock_gettime(CLOCK_REALTIME, &start); } node_id = fastrand(&seed) & NUM_NODES_; lock_id = node_id & NUM_LOCKS_; for(j = 0; j < NUM_WRITER_LOCKS; j ++) { int lock_index = (lock_id + (j * WRITER_LOCK_STRIDE)) & NUM_LOCKS_; pthread_spin_lock(&locks[lock_index].lock); } /** < Update node.a and node.b after some expensive computation */ for(i = 0; i < WRITER_COMPUTE; i ++) { nodes[node_id].a = CityHash32((char *) &nodes[node_id].a, 4); } nodes[node_id].b = nodes[node_id].a + 1; for(j = 0; j < NUM_WRITER_LOCKS; j ++) { int lock_index = (lock_id + (j * WRITER_LOCK_STRIDE)) & NUM_LOCKS_; pthread_spin_unlock(&locks[lock_index].lock); } num_iters ++; } }
void *writer( void *ptr) { struct timespec start, end; int tid = *((int *) ptr); uint64_t seed = 0xdeadbeef + tid; int sum = 0; /** < The node and lock to use in an iteration */ int i, node_id, lock_id; /** < Total number of iterations (for measurement) */ int num_iters = 0; clock_gettime(CLOCK_REALTIME, &start); while(1) { if(num_iters == ITERS_PER_MEASUREMENT) { clock_gettime(CLOCK_REALTIME, &end); double seconds = (end.tv_sec - start.tv_sec) + (double) (end.tv_nsec - start.tv_nsec) / GHZ_CPS; node_id = fastrand(&seed) & NUM_NODES_; red_printf("Writer thread %d: rate = %.2f M/s. " "Random node: (%lld, %lld)\n", tid, num_iters / (1000000 * seconds), nodes[node_id].a, nodes[node_id].b); num_iters = 0; clock_gettime(CLOCK_REALTIME, &start); } node_id = fastrand(&seed) & NUM_NODES_; lock_id = node_id & NUM_LOCKS_; locks[lock_id].lock ++; /** < version store #1 --> node stores */ asm volatile("" ::: "memory"); /** < Update node.a and node.b after some expensive computation */ for(i = 0; i < WRITER_COMPUTE; i ++) { nodes[node_id].a = CityHash32((char *) &nodes[node_id].a, 4); } nodes[node_id].b = nodes[node_id].a + 1; /** < node stores --> version store #2 */ asm volatile("" ::: "memory"); locks[lock_id].lock ++; num_iters ++; } }
int main(int argc, char **argv) { int i; /** < Variables for PAPI */ float real_time, proc_time, ipc; long long ins; int retval; red_printf("main: Initializing cuckoo hash table\n"); cuckoo_init(&keys, &ht_index); red_printf("main: Starting lookups\n"); /** < Init PAPI_TOT_INS and PAPI_TOT_CYC counters */ if((retval = PAPI_ipc(&real_time, &proc_time, &ins, &ipc)) < PAPI_OK) { printf("PAPI error: retval: %d\n", retval); exit(1); } for(i = 0; i < NUM_KEYS; i += BATCH_SIZE) { process_batch(&keys[i]); } if((retval = PAPI_ipc(&real_time, &proc_time, &ins, &ipc)) < PAPI_OK) { printf("PAPI error: retval: %d\n", retval); exit(1); } red_printf("Time = %.4f s, rate = %.2f\n" "Instructions = %lld, IPC = %f\n" "sum = %d, succ_1 = %d, succ_2 = %d, fail = %d\n", real_time, NUM_KEYS / real_time, ins, ipc, sum, succ_1, succ_2, fail); return 0; }
int main(int argc, char **argv) { int i; /** < Variables for PAPI */ float real_time, proc_time, ipc; long long ins; int retval; red_printf("main: Initializing nodes for random walk\n"); rand_walk_init(&nodes); red_printf("main: Starting random walks\n"); /** < Init PAPI_TOT_INS and PAPI_TOT_CYC counters */ if((retval = PAPI_ipc(&real_time, &proc_time, &ins, &ipc)) < PAPI_OK) { printf("PAPI error: retval: %d\n", retval); exit(1); } /** < Do a random-walk from every node in the graph */ for(i = 0; i < NUM_NODES; i += BATCH_SIZE) { process_batch(&nodes[i]); } if((retval = PAPI_ipc(&real_time, &proc_time, &ins, &ipc)) < PAPI_OK) { printf("PAPI error: retval: %d\n", retval); exit(1); } red_printf("Time = %.4f, rate = %.2f sum = %lld\n" "Instructions = %lld, IPC = %f\n", real_time, NUM_NODES / real_time, sum, ins, ipc); return 0; }
int main(int argc, char **argv) { printf("%lu\n", sizeof(struct ndn_bucket)); struct ndn_bucket *ht; int i, j; int dst_ports[BATCH_SIZE], nb_succ = 0, dst_port_sum = 0; /** < Variables for PAPI */ float real_time, proc_time, ipc; long long ins; int retval; red_printf("main: Initializing NDN hash table\n"); ndn_init(URL_FILE, 0xf, &ht); red_printf("\tmain: Setting up NDN index done!\n"); red_printf("main: Getting name array for lookups\n"); int nb_names = ndn_get_num_lines(NAME_FILE); nb_names = nb_names - (nb_names % BATCH_SIZE); /**< Align input to batch */ struct ndn_name *name_arr = ndn_get_name_array(NAME_FILE); red_printf("\tmain: Constructed name array!\n"); red_printf("main: Starting NDN lookups\n"); /** < Init PAPI_TOT_INS and PAPI_TOT_CYC counters */ if((retval = PAPI_ipc(&real_time, &proc_time, &ins, &ipc)) < PAPI_OK) { printf("PAPI error: retval: %d\n", retval); exit(1); } for(i = 0; i < nb_names; i += BATCH_SIZE) { memset(dst_ports, -1, BATCH_SIZE * sizeof(int)); process_batch(&name_arr[i], dst_ports, ht); for(j = 0; j < BATCH_SIZE; j ++) { #if NDN_DEBUG == 1 printf("Name %s -> port %d\n", name_arr[i + j].name, dst_ports[j]); #endif nb_succ += (dst_ports[j] == -1) ? 0 : 1; dst_port_sum += dst_ports[j]; } } if((retval = PAPI_ipc(&real_time, &proc_time, &ins, &ipc)) < PAPI_OK) { printf("PAPI error: retval: %d\n", retval); exit(1); } red_printf("Time = %.4f s, Lookup rate = %.2f M/s | nb_succ = %d, sum = %d\n" "Instructions = %lld, IPC = %f\n", real_time, nb_names / (real_time * 1000000), nb_succ, dst_port_sum, ins, ipc); return 0; }
static void smartcard_char_device_attach( SpiceCharDeviceInstance *char_device, SmartCardChannel *smartcard_channel) { SmartCardDeviceState *st = SPICE_CONTAINEROF(char_device->st, SmartCardDeviceState, base); if (st->attached == TRUE) { return; } st->attached = TRUE; VSCMsgHeader vheader = {.type = VSC_ReaderAdd, .reader_id=st->reader_id, .length=0}; smartcard_channel_write_to_reader(smartcard_channel, &vheader); } static void smartcard_char_device_detach( SpiceCharDeviceInstance *char_device, SmartCardChannel *smartcard_channel) { SmartCardDeviceState *st = SPICE_CONTAINEROF(char_device->st, SmartCardDeviceState, base); if (st->attached == FALSE) { return; } st->attached = FALSE; VSCMsgHeader vheader = {.type = VSC_ReaderRemove, .reader_id=st->reader_id, .length=0}; smartcard_channel_write_to_reader(smartcard_channel, &vheader); } static int smartcard_channel_config_socket(RedChannel *channel) { return TRUE; } static uint8_t *smartcard_channel_alloc_msg_rcv_buf(RedChannel *channel, SpiceDataHeader *msg_header) { //red_printf("allocing %d bytes", msg_header->size); return spice_malloc(msg_header->size); } static void smartcard_channel_release_msg_rcv_buf(RedChannel *channel, SpiceDataHeader *msg_header, uint8_t *msg) { red_printf("freeing %d bytes", msg_header->size); free(msg); }
int main(int argc, char *argv[]) { pthread_t thread[CPU_MAX_THREADS]; int i, num_threads; int *log; assert(argc == 2); num_threads = atoi(argv[1]); assert(num_threads >= 1 && num_threads <= CPU_MAX_THREADS); /**< We follow 8 streams in one shot */ assert(CPU_NUM_STREAMS % 8 == 0); /**< Initialize hugepage log for all CPU threads */ red_printf("Allocating host log of size %lu bytes\n", LOG_CAP * sizeof(int)); int sid = shmget(LOG_KEY, LOG_CAP * sizeof(int), SHM_HUGETLB | 0666 | IPC_CREAT); assert(sid >= 0); log = (int *) shmat(sid, 0, 0); assert(log != NULL); init_ht_log(log, LOG_CAP); /**< Start all CPU threads */ struct thread_info ti[CPU_MAX_THREADS]; for(i = 0; i < num_threads; i ++) { ti[i].tid = i; ti[i].log = log; pthread_create(&thread[i], NULL, cpu_func, (void *) &ti[i]); /**< Allow threads to go out of sync */ usleep(100000); } /**< Wait till the sun rises in the west and sets in the east */ for(i = 0; i < num_threads; i ++) { pthread_join(thread[i], NULL); } }
static int smartcard_channel_handle_message(RedChannel *channel, SpiceDataHeader *header, uint8_t *msg) { VSCMsgHeader* vheader = (VSCMsgHeader*)msg; SmartCardChannel* smartcard_channel = (SmartCardChannel*)channel; ASSERT(header->size == vheader->length + sizeof(VSCMsgHeader)); switch (vheader->type) { case VSC_ReaderAdd: smartcard_add_reader(smartcard_channel, msg + sizeof(VSCMsgHeader)); return TRUE; break; case VSC_ReaderRemove: smartcard_remove_reader(smartcard_channel, vheader->reader_id); return TRUE; break; case VSC_ReaderAddResponse: /* We shouldn't get this - we only send it */ return TRUE; break; case VSC_Init: case VSC_Error: case VSC_ATR: case VSC_CardRemove: case VSC_APDU: break; // passed on to device default: printf("ERROR: unexpected message on smartcard channel\n"); return TRUE; } if (vheader->reader_id >= g_smartcard_readers.num) { red_printf("ERROR: received message for non existent reader: %d, %d, %d", vheader->reader_id, vheader->type, vheader->length); return FALSE; } smartcard_channel_write_to_reader(smartcard_channel, vheader); return TRUE; }
void *ids_func(void *ptr) { int i, j; struct aho_ctrl_blk *cb = (struct aho_ctrl_blk *) ptr; int id = cb->tid; struct aho_dfa *dfa_arr = cb->dfa_arr; struct aho_pkt *pkts = cb->pkts; int num_pkts = cb->num_pkts; /**< Per-batch matched patterns */ struct mp_list_t mp_list[BATCH_SIZE]; for(i = 0; i < BATCH_SIZE; i ++) { mp_list[i].num_match = 0; } /**< Being paranoid about GCC optimization: ensure that the memcpys in * process_batch functions don't get optimized out */ int matched_pat_sum = 0; int tot_proc = 0; /**< How many packets did we actually match ? */ int tot_success = 0; /**< Packets that matched a DFA state */ int tot_bytes = 0; /**< Total bytes matched through DFAs */ while(1) { struct timespec start, end; clock_gettime(CLOCK_REALTIME, &start); for(i = 0; i < num_pkts; i += BATCH_SIZE) { process_batch(dfa_arr, &pkts[i], mp_list); for(j = 0; j < BATCH_SIZE; j ++) { int num_match = mp_list[j].num_match; assert(num_match < MAX_MATCH); tot_success += num_match == 0 ? 0 : 1; int pat_i; #if DEBUG == 1 printf("Pkt %d matched: ", pkts[i + j].pkt_id); for(pat_i = 0; pat_i < num_match; pat_i ++) { printf("%d ", mp_list[j].ptrn_id[pat_i]); matched_pat_sum += mp_list[j].ptrn_id[pat_i]; } printf("\n"); #else for(pat_i = 0; pat_i < num_match; pat_i ++) { matched_pat_sum += mp_list[j].ptrn_id[pat_i]; } #endif tot_proc ++; tot_bytes += pkts[i + j].len; /**< Re-initialize for next iteration */ mp_list[j].num_match = 0; } } clock_gettime(CLOCK_REALTIME, &end); double ns = (end.tv_sec - start.tv_sec) * 1000000000 + (double) (end.tv_nsec - start.tv_nsec); red_printf("ID %d: Rate = %.2f Gbps. tot_success = %d\n", id, ((double) tot_bytes * 8) / ns, tot_success); red_printf("num_pkts = %d, tot_proc = %d | matched_pat_sum = %d\n", num_pkts, tot_proc, matched_pat_sum); matched_pat_sum = 0; /**< Sum of all matched pattern IDs */ tot_success = 0; tot_bytes = 0; tot_proc = 0; #if DEBUG == 1 /**< Print matched states only once */ exit(0); #endif } }
int main(int argc, char *argv[]) { assert(argc == 2); int num_threads = atoi(argv[1]); assert(num_threads >= 1 && num_threads <= AHO_MAX_THREADS); int num_patterns, num_pkts, i; struct aho_pattern *patterns; struct aho_pkt *pkts; struct aho_dfa dfa_arr[AHO_MAX_DFA]; /**< Thread structures */ pthread_t worker_threads[AHO_MAX_THREADS]; struct aho_ctrl_blk worker_cb[AHO_MAX_THREADS]; red_printf("State size = %lu\n", sizeof(struct aho_state)); /**< Initialize the shared DFAs */ for(i = 0; i < AHO_MAX_DFA; i ++) { printf("Initializing DFA %d\n", i); aho_init(&dfa_arr[i], i); } red_printf("Adding patterns to DFAs\n"); patterns = aho_get_patterns(AHO_PATTERN_FILE, &num_patterns); for(i = 0; i < num_patterns; i ++) { int dfa_id = patterns[i].dfa_id; aho_add_pattern(&dfa_arr[dfa_id], &patterns[i], i); } red_printf("Building AC failure function\n"); for(i = 0; i < AHO_MAX_DFA; i ++) { aho_build_ff(&dfa_arr[i]); aho_preprocess_dfa(&dfa_arr[i]); } red_printf("Reading packets from file\n"); pkts = aho_get_pkts(AHO_PACKET_FILE, &num_pkts); for(i = 0; i < num_threads; i ++) { worker_cb[i].tid = i; worker_cb[i].dfa_arr = dfa_arr; worker_cb[i].pkts = pkts; worker_cb[i].num_pkts = num_pkts; pthread_create(&worker_threads[i], NULL, ids_func, &worker_cb[i]); /**< Ensure that threads don't use the same packets close in time */ sleep(1); } for(i = 0; i < num_threads; i ++) { pthread_join(worker_threads[i], NULL); } /**< The work never ends */ assert(0); return 0; }
int test2() { printf("Before\n"); red_printf("Hello %s %s!\n", "world", "young man"); printf("Hehe\n"); return 0; }