Beispiel #1
0
static void router_test2(void)
{
	int i;
	struct node_t* node;
	struct node_t* result[8];
	static struct node_t s_nodes[100000];
	uint8_t id[N_NODEID] = { 0xAB, 0xCD, 0xEF, 0x89, };

	heap_t* heap, *heap2;
	struct router_t* router;
	router = router_create(id);

	heap = heap_create(node_compare_less, (void*)id);
	heap_reserve(heap, 8 + 1);

	for (i = 0; i < sizeof(s_nodes) / sizeof(s_nodes[0]); i++)
	{
		int v = rand();

		memset(&s_nodes[i], 0, sizeof(s_nodes[i]));
		memcpy(s_nodes[i].id, &v, sizeof(v));
		s_nodes[i].ref = 1;

		if (0 == router_add(router, s_nodes[i].id, &s_nodes[i].addr, &node))
		{
			heap_push(heap, node);
			if (heap_size(heap) > 8)
			{
				node_release((struct node_t*)heap_top(heap));
				heap_pop(heap);
			}
		}
	}

	assert(8 == heap_size(heap));
	assert(8 == router_nearest(router, id, result, 8));

	heap2 = heap_create(node_compare_less, (void*)id);
	heap_reserve(heap2, 8);

	for (i = 0; i < 8; i++)
	{
		heap_push(heap2, result[i]);
	}

	assert(heap_size(heap) == heap_size(heap2));
	for (i = 0; i < 8; i++)
	{
		assert(heap_top(heap2) == heap_top(heap));
		heap_pop(heap);
		heap_pop(heap2);
	}

	router_destroy(router);
	heap_destroy(heap);
	heap_destroy(heap2);
	printf("router test ok!\n");
}
int search(struct arg_struct *arg) {
  // printf("# Thread working: %u\n", (int)pthread_self());
  int n = arg->topic;
  int start = arg->startidx;
  int end = arg->endidx;
  heap* h = arg->h;
  heap_create(h,0,NULL);

  int i=0, j=0;
  int base = arg->base;
  float score;
  int t;
  float* min_key;
  int* min_val;

  for (i=start; i<end; i++) {
    if (tweetids[i] > topics2011_time[n]) {
      base += doclengths[i];
      continue;
    }
    score = 0;
    int tf[topics2011[n][1]];
    memset(tf,0,sizeof(tf));
    for (j=0; j<doclengths[i]; j++) {
      for (t=2; t<2+topics2011[n][1]; t++) {
        if (collection_pos[base+j] == topics2011[n][t]) {
          tf[t - 2] ++;
        }
      }
    }
    for (t = 0; t < topics2011[n][1]; t++) {
      if (tf[t] > 0) {
        score += log(1 + tf[t]/(MU * (cf[topics2011[n][t+2]] + 1) / (TOTAL_TERMS + 1))) + log(MU / (doclengths[i] + MU));
      }
    }

    if (score > 0) {
      int size = heap_size(h);

      if ( size < TOP_K ) {
        int *docid = malloc(sizeof(int)); *docid = i;
        float *scorez = malloc(sizeof(float)); *scorez = score;
        heap_insert(h, scorez, docid);
      } else {
        heap_min(h, (void**)&min_key, (void**)&min_val);

        if (score > *min_key) {
          heap_delmin(h, (void**)&min_key, (void**)&min_val);

          int *docid = malloc(sizeof(int)); *docid = i;
          float *scorez = malloc(sizeof(float)); *scorez = score;
          heap_insert(h, scorez, docid);
        }
      }
    }

    base += doclengths[i];
  }
  return 0;
}
Beispiel #3
0
// Initialise the memory subsystem
void mem_init(int argc, char** argv) {
	(void)argc; (void)argv;
	static_heap = heap_create( static_heap_size );
	// Start with two simple bitpools
	heap_addBitpool( static_heap, 16, 16384 );
	heap_addBitpool( static_heap, 64, 16384 );
}
Beispiel #4
0
int main(void)
{
	struct AATree aatree;
	struct CBTree *cbtree;
	struct md5_ctx md5;
	struct Heap *heap;
	char buf[128];

	static_assert(sizeof(int) >= 4, "unsupported int size");

	heap = heap_create(heap_is_better, NULL, NULL);
	heap_top(heap);
	aatree_init(&aatree, NULL, NULL);
	cbtree = cbtree_create(NULL, NULL, NULL, NULL);
	cbtree_destroy(cbtree);
	daemonize(NULL, false);
	hash_lookup3("foo", 3);
	if (!event_init())
		log_debug("test");
	if (!parse_ini_file("foo", NULL, NULL))
		log_debug("test");
	log_stats("1");
	file_size("foo");
	md5_reset(&md5);
	strlcpy(buf, "foo", sizeof(buf));
	printf("xmalloc: %p\n", xmalloc(128));
	if (0) die("0");
	csrandom();
	tls_init();
	return 0;
}
Beispiel #5
0
etimer_t * etimer_create()
{
    etimer_t *timer;
    int pipefd[2], flags;
    struct epoll_event ev;

    timer = malloc(sizeof(etimer_t));
    memset(timer, 0, sizeof(*timer));

    timer->epfd = epoll_create(10);
    pthread_mutex_init(&timer->lock, NULL);

    pipe(pipefd);
    flags = fcntl(pipefd[0], F_GETFL, 0);
    fcntl(pipefd[0], F_SETFL, flags|O_NONBLOCK);
    timer->pin = pipefd[0];
    timer->pout = pipefd[1];

    memset(&ev, 0, sizeof(ev));
    ev.data.fd = timer->pin;
    ev.events = EPOLLIN|EPOLLET;
    epoll_ctl(timer->epfd, EPOLL_CTL_ADD, timer->pin, &ev);

    timer->queue = heap_create(etimer_event_cmp);

    return timer;
}
Beispiel #6
0
struct workqueue *workqueue_create(struct workqueue *wq, int flags)
{
	KOBJ_CREATE(wq, flags, WORKQUEUE_KMALLOC);
	heap_create(&wq->tasks, HEAP_LOCKLESS, HEAPMODE_MAX);
	spinlock_create(&wq->lock);
	return wq;
}
Beispiel #7
0
void test_heap_test_1(void) {

    char heap_mem[heap_mem_size(16, sizeof(uint32_t))];
    struct heap *h = heap_create( heap_mem
                                , sizeof(heap_mem)
                                , sizeof(uint32_t)
                                , u32_leq
                                , u32_cpy );

    fprintf(stdout, "# heap_size: %ld\n", heap_size(h));
    fprintf(stdout, "# heap_empty: %s\n", heap_empty(h) ? "yes" : "no");

    uint32_t vs[] = {9,100,7,2,5,200,1,20,8,8,8,150};
    size_t i = 0;
    for(; i < sizeof(vs)/sizeof(vs[0]); i++ ) {
        if( !heap_add(h, &vs[i]) ) {
            break;
        }
    }

    fprintf(stdout, "# heap_empty: %s\n", heap_empty(h) ? "yes" : "no");

    while( !heap_empty(h) ) {
        uint32_t *mp = heap_pop(h);
        if( !mp )
            break;
        fprintf(stdout, "# %d\n", *mp);
    }
}
Beispiel #8
0
struct evg_compute_unit_t *evg_compute_unit_create()
{
	struct evg_compute_unit_t *compute_unit;
	char buf[MAX_STRING_SIZE];

	/* Initialize */
	compute_unit = xcalloc(1, sizeof(struct evg_compute_unit_t));
	compute_unit->wavefront_pool = linked_list_create();

	/* Local memory */
	snprintf(buf, sizeof buf, "LocalMemory[%d]", compute_unit->id);
	compute_unit->local_memory = mod_create(buf, mod_kind_local_memory,
		evg_gpu_local_mem_num_ports, evg_gpu_local_mem_block_size, evg_gpu_local_mem_latency);

	/* Initialize CF Engine */
	compute_unit->cf_engine.complete_queue = linked_list_create();
	compute_unit->cf_engine.fetch_buffer = xcalloc(evg_gpu_max_wavefronts_per_compute_unit, sizeof(void *));
	compute_unit->cf_engine.inst_buffer = xcalloc(evg_gpu_max_wavefronts_per_compute_unit, sizeof(void *));

	/* Initialize ALU Engine */
	compute_unit->alu_engine.pending_queue = linked_list_create();
	compute_unit->alu_engine.finished_queue = linked_list_create();
	compute_unit->alu_engine.fetch_queue = linked_list_create();
	compute_unit->alu_engine.event_queue = heap_create(10);

	/* Initialize TEX Engine */
	compute_unit->tex_engine.pending_queue = linked_list_create();
	compute_unit->tex_engine.finished_queue = linked_list_create();
	compute_unit->tex_engine.fetch_queue = linked_list_create();
	compute_unit->tex_engine.load_queue = linked_list_create();

	/* Return */
	compute_unit->work_groups = xcalloc(evg_gpu_max_work_groups_per_compute_unit, sizeof(void *));
	return compute_unit;
}
Beispiel #9
0
struct ticker *ticker_create(struct ticker *ticker, int flags)
{
	KOBJ_CREATE(ticker, flags, TICKER_KMALLOC);
	ticker->tick = 0;
	heap_create(&ticker->heap, HEAP_LOCKLESS, HEAPMODE_MIN);
	spinlock_create(&ticker->lock);
	return ticker;
}
Beispiel #10
0
/**
 * @brief Dijkstra's shortest path algorithm 
 *
 * @param[in] g The graph to operate on
 * @param[in] s The starting vertex
 * @param[in] cb The function to call when a shortest spath vertex is determined.  
 */
void sp_dijkstra (GRAPH_T* g, unsigned long s, SP_DJ_FP_T cb)
{
   HEAP_T* h;
   VTX_D_T* u = NULL;
   VTX_D_T* v;
   unsigned long key, no;
   char* ctx = NULL;
   EDGE_T* e;
   void* p;
   
   initialize_single_source (g, s);
   h = heap_create (DS_HEAP_MIN, GRAPH_NO_VERTICES(g));

   while (NULL != (u = graph_vertex_next_get (g, u)))
   {
      heap_min_insert (h, D_SP_AUX_SPEST(u), u, &D_SP_AUX_I(u));
   }

   while (HEAP_SIZE(h))
   {
      heap_extract_min (h, &p, &key);
      u = (VTX_D_T*)p;

      ctx = NULL;
      no = ((VTX_D_T*)u)->no;

      while (no)
      {
         e = graph_vertex_next_edge_get (g, u, &ctx);
         if (e->v1 == u)
            v = e->v2;
         else
            v = e->v1;
         if (v->id.iid == s)
         {
            no--;
            continue;
         }
         DEBUG_PRINT ("Relaxing v=%lu (OLD weight: %lu; NEW weight: u=%lu w=%lu)\n",
                 v->id.iid, D_SP_AUX_SPEST(v), u->id.iid, e->weight);
         relax (g, u, v, e->weight);
         heap_decrease_key (h, D_SP_AUX_I(v), D_SP_AUX_SPEST(v));
         no--;
      }
   }

   if (cb)
   {
      v = NULL;
      while (NULL != (v = graph_vertex_next_get (g, v)))
      {
         cb (v);
         //fprintf (stderr, "vid = %lu sp=%lu\n", v->id.iid, D_SP_AUX_SPEST(v));
      }
   }
}
Beispiel #11
0
void timers_init(void)
{
    if (initialized == 0) {
        timers = gw_malloc(sizeof(*timers));
        timers->mutex = mutex_create();
        timers->heap = heap_create();
        timers->stopping = 0;
        timers->thread = gwthread_create(watch_timers, timers);
    }
    initialized++;
}
Beispiel #12
0
int main(int argc, char * argv[])
{
  int length, num, c;
  int * array;
  heap h;
  if (argc != 3) {
    fprintf(stderr,"Usage: %s <heap-length> <num-tests>\n", argv[0]);
    return 1;
  }
  length = atoi(argv[1]);
  num = atoi(argv[2]);

  if (length <= 0) {
    fprintf(stderr,"Error: length <= 0\n");
    return 1;    
  }

  h = heap_create();
  
  array = ALLOC(int,length);
  nusmv_assert(array);

  for (c = 1; c <= num; c++) {
    int i, i1, i2;
    printf("Test %2d:", c);
    for (i = 0; i < length; i++) {
      array[i] = i+1;
    }
    for (i = 0; i < 4 * length; i++) {
      i1 = utils_random() % length;
      i2 = utils_random() % length;
      if (i1 == i2) continue;
      array[i1] = array[i1] + array[i2];
      array[i2] = array[i1] - array[i2];
      array[i1] = array[i1] - array[i2];
    }
    for (i = 0; i < length; i++) {
      printf(" %d", array[i]);
      heap_add(h, -array[i], (void *)array[i]);
    }
    printf("\n------->");
    for (i = 0; i < length; i++) {
      int val = (int)heap_getmax(h);
      printf(" %d", val);
      nusmv_assert(val == i+1);
    }
    printf("\n");
    assert(heap_isempty(h));
  }

  heap_destroy(h);

  return 0;
}
Beispiel #13
0
int main()
{
  struct MinHeap *h = heap_create(11);
  push(h,3);
  push(h,2);
  push(h,5);
  while(!isEmpty(h))
  {
    printf(" %d ",pop(h));
  }
}
Beispiel #14
0
void test_heap_test_4(void) {

    size_t memsize = heap_mem_size(1, sizeof(struct cw));

    fprintf(stderr, "memsize: %ld\n", memsize);

    char heap_mem[heap_mem_size(1, sizeof(struct cw))];
    struct heap *h = heap_create( heap_mem
                                , sizeof(heap_mem)
                                , sizeof(struct cw)
                                , __cw_leq
                                , __cw_cpy );

    fprintf(stderr, "heap: %p\n", h);

    fprintf(stdout, "# heap_size: %ld\n", heap_size(h));

    struct cw cats[] = { {  1, 1 }
                       , {  2, 1 }
                       , {  1, 2 }
                       , {  3, 1 }
                       , { 12, 3 }
                       , {  5, 1 }
                       , { 31, 2 }
                       , {  6, 2 }
                       , {  7, 1 }
                       , {  7, 1 }
                       , { 10, 5 }
                       };

    fprintf(stdout, "\n");


    size_t i = 0;
    for(; i < sizeof(cats)/sizeof(cats[0]); i++ ) {
        fprintf(stdout, "# {%d, %d}\n", cats[i].cat, cats[i].weight);
        if( heap_full(h) ) {
            struct cw *min = heap_get(h);
            if( __cw_leq(min, &cats[i]) ) {
                heap_pop(h);
            }
        }
        heap_add(h, &cats[i]);
    }

    fprintf(stdout, "\nheap_items %ld\n", heap_items(h));

    fprintf(stdout, "\n");
    while( !heap_empty(h) ) {
        struct cw *c = heap_pop(h);
        fprintf(stdout, "# {%d, %d}\n", c->cat, c->weight);
    }
}
Beispiel #15
0
Timerset *gw_timerset_create(void)
{
	Timerset *set;

	set = gw_malloc(sizeof(Timerset));
    set->mutex = mutex_create();
    set->heap = heap_create();
    set->stopping = 0;
    set->thread = gwthread_create(watch_timers, set);

    return set;
}
Beispiel #16
0
void heapSort(int arr[],int n)
{
  struct MinHeap* heap = heap_create(n);
  for(int i=0;i<n;i++)
  {
    push(heap,arr[i]);
  }
  int i=0;
  while(!isEmpty(heap))
  {
    arr[i++]=pop(heap);
  }
}
Beispiel #17
0
/* removes the element of minimum weight or does nothing in case of empty heap */
heap heap_remove_min(heap h)
{
	if( h->root == NULL ) return NULL;

	tree sl = h->root->left;
	tree sr = h->root->right;
	free(h->root);
	h->root = sl;
	heap tmph = heap_create(h->f);
	tmph->root = sr;
	h = heap_merge(h, tmph);
	// free(tmph);
	return h;
}
Beispiel #18
0
/* insert an element in the heap or NULL if object is NULL */
heap heap_insert(heap h, void* object)
{
	if( object == NULL) return NULL;

	tree nt = malloc(sizeof(*nt));
	nt->left = NULL;
	nt->right = NULL;
	nt->object = object;

	heap tmph = heap_create(h->f);
	tmph->root = nt;
	h = heap_merge(h, tmph );
	// free(tmph);
	return h;
}
Beispiel #19
0
int main(int argc, char *argv[])
{
    Module *m;
    Thread t;
    const char *file = argc > 1 ? argv[1] : "out.rmd";
    loader_add_dir_from_file(file);
    loader_add_dirs_from_env();
    heap_create();

    m = module_load_obligatory(file);
    if(thread_init(&t, m, "main") < 0)
        return 1;
    thread_run(&t);

    return 0;
}
Beispiel #20
0
void ShortestPath_Dijekstra(struct graph *g, int src, int *d, int *prev)
{
    struct heap *prio_q;
    prio_q = heap_create(g->nvertices);
    
    int v;
    
    for (v = 1; v <= g->nvertices; v++) {
	if (v != src) {
	    g->visited[v] = 0;
	    d[v] = INT_MAX;
	    prev[v] = -1;
	    heap_insert(prio_q, d[v], v);
	}
    }
    
    d[src] = 0;
    prev[src] = -1;
    g->visited[src] = 0;
    heap_insert(prio_q, d[src], src);
    
    struct heapnode node;
    int tmp;
    for (v = 1; v <= g->nvertices; v++) {
	node = heap_extract_min(prio_q);
	tmp = node.value;
	g->visited[tmp] = 1;
	/*printf("vert %d\n\n", v);
	printf("min prio %d to vert %d\n", node.key, node.value);*/

	for (int u  = 1; u <= g->nvertices; u++) {
	    
	    int way = graph_get_edge(g, tmp, u);
	    if ((way != 0) && (g->visited[u] != 1)) {
		//printf("sm ne pos vert %d\nway %d\n", u, way);
		if (d[tmp] + way < d[u]) {
		    d[u] = d[tmp] + way;
		    //printf("path do %d = %d\n", u, d[u]);
		    heap_decrease_key(prio_q, u, d[u]);
		    prev[u] = tmp;
		}
	    }
	}
    }
}
// Implementation from:
// https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm#Using_a_priority_queue
void dijkstra(list_t graph, int width, int height, point_t* source)
{
    // set distance to self to 0
    source->prev = source;
    source->dist = 0;

    // create priority queue
    heap_t queue;
    heap_create(&queue, width * height);

    heap_insert(queue, 0, source);

    while (heap_size(queue) > 0)
    {
        // find vertex u with minimal cost
        point_t* u;
        heap_remove(queue, &u);

        // for neighbour v to u
        for (int i = MAX(u->x - 1, 0); i <= MIN(u->x + 1, width - 1); ++i)
        {
            for (int j = MAX(u->y - 1, 0); j <= MIN(u->y + 1, height - 1); ++j)
            {
                if (i != u->x || j != u->y) // u can't be its own neighbour
                {
                    point_t* v = graph_find(graph, width, height, i, j);

                    // is the current distance to u shorter through v?
                    int alt = u->dist + v->cost;

                    if (alt < v->dist) 
                    {
                        v->dist = alt;
                        v->prev = u;

                        heap_insert(queue, alt, v);
                    }
                }
            }
        }
    }

    heap_free(queue, NULL, NULL);
}
Beispiel #22
0
void sortK(int a[],int n,int k)
{
  struct MinHeap *heap = heap_create(k+1);
  for(int i=0;i<k+1;i++)
  {
    push(heap,a[i]);
  }
  for(int i=k+1,ti=0;ti<n;i++,ti++)
  {
    if(i<n)
    {
      a[ti] = replacetop(heap,a[i]);
    }
    else
    {
      a[ti] = pop(heap);
    }
  }
}
Beispiel #23
0
void esim_init()
{
	/* Create structures */
	esim_event_info_list = list_create();
	esim_event_heap = heap_create(20);
	esim_end_event_list = linked_list_create();
	
	/* List of frequency domains */
	esim_domain_list = list_create();
	list_add(esim_domain_list, NULL);

	/* Initialize global timer */
	esim_timer = m2s_timer_create(NULL);
	m2s_timer_start(esim_timer);

	/* Register special events */
	ESIM_EV_INVALID = esim_register_event_with_name(NULL, 0, "Invalid");
	ESIM_EV_NONE = esim_register_event_with_name(NULL, 0, "None");
}
Beispiel #24
0
static bool
cmd_test_heap()
{
    pagetable_t pt;
    pagetable_create(&pt, (void *)0x8000000000, PAGE_SIZE * 1024);
    pagetable_activate(&pt);

    struct heap *heap = heap_create(&pt, (void *)0x9000000000, 1024);
    void        *ptr1 = heap_alloc(heap, 128);
    void        *ptr2 = heap_alloc(heap, 0xff00);
    void        *ptr3 = heap_alloc(heap, 8);
    heap_free(heap, ptr1);
    heap_free(heap, ptr2);
    heap_free(heap, ptr3);

    heap_destroy(heap);
    pagetable_activate(NULL);
    pagetable_destroy(&pt);
    return true;
}
Beispiel #25
0
void kLargest(int a[],int n,int k)
{
  struct MinHeap *heap = heap_create(k);
  for(int i=0;i<k;i++)
  {
    push(heap,a[i]);
  }
  for(int i=k;i<n;i++)
  {
    if(a[i] > peek(heap))
    {
      pop(heap);
      push(heap,a[i]);
    }
  }
  while(!isEmpty(heap))
  {
    printf(" %d ",pop(heap));
  }
}
Beispiel #26
0
int main(int argc, char **argv) {
    int i;
    heap_t *h = heap_create();
    h->less = less;
    srand(time(NULL));
    for (i = 0; i < TEST_NUMBER; ++i) {
        assert(heap_insert(h, (void *)(long)(rand() % TEST_NUMBER)) == 0);
    }

    printf("Original\n==========================================\n");
    for (i = 0; i < h->len; ++i) {
        printf("%ld\n", (long)h->data[i]);
    }

    printf("Sorted\n==========================================\n");
    while (h->len != 0) {
        void *value = heap_remove(h, 0);
        printf("%ld\n", (long)value);
    }
    heap_free(h);
}
Beispiel #27
0
Relation
copy_heap(Oid OIDOldHeap)
{
    char NewName[NAMEDATALEN];
    TupleDesc OldHeapDesc, tupdesc;
    Oid OIDNewHeap;
    Relation NewHeap, OldHeap;

    /*
     *  Create a new heap relation with a temporary name, which has the
     *  same tuple description as the old one.
     */
    sprintf(NewName,"temp_%x", OIDOldHeap);

    OldHeap= heap_open(OIDOldHeap);
    OldHeapDesc= RelationGetTupleDescriptor(OldHeap);

    /*
     * Need to make a copy of the tuple descriptor, heap_create modifies
     * it.
     */

    tupdesc = CreateTupleDescCopy(OldHeapDesc);
    
    OIDNewHeap=heap_create(NewName,
			   NULL,
			   OldHeap->rd_rel->relarch,
			   OldHeap->rd_rel->relsmgr,
			   tupdesc);

    if (!OidIsValid(OIDNewHeap))
	elog(WARN,"clusterheap: cannot create temporary heap relation\n");

    NewHeap=heap_open(OIDNewHeap);

    heap_close(NewHeap);
    heap_close(OldHeap);

    return NewHeap;
}
Beispiel #28
0
void test_heap_test_2(void) {

    char heap_mem[heap_mem_size(10, sizeof(uint32_t))];
    struct heap *h = heap_create( heap_mem
                                , sizeof(heap_mem)
                                , sizeof(uint32_t)
                                , u32_leq
                                , u32_cpy );

    fprintf(stdout, "# heap_size: %ld\n", heap_size(h));

    size_t N = 3000;

    size_t n = 0;
    for(n = N; n; n-- ) {
        __test_head_add_gt(h, n);
    }

    fprintf(stdout, "\n");
    while( !heap_empty(h) ) {
        uint32_t *mp = heap_pop(h);
        if( !mp )
            break;
        fprintf(stdout, "# %d\n", *mp);
    }

    for(n = 0; n <= N; n++ ) {
        __test_head_add_gt(h, n);
    }

    fprintf(stdout, "\n");
    while( !heap_empty(h) ) {
        uint32_t *mp = heap_pop(h);
        if( !mp )
            break;
        fprintf(stdout, "# %d\n", *mp);
    }
}
int search(int n) {
  struct arg_struct *args = malloc(sizeof (struct arg_struct));
  args->topic = n;
  args->startidx = 0;
  args->endidx = num_docs;
  args->base = 0;
  heap h;
  heap_create(&h,0,NULL);
  args->h = &h;
  
  scansearch(args);
  
  float* min_key;
  int* min_val;
  int rank = TOP_K;
  while (heap_delmin(&h, (void**)&min_key, (void**)&min_val)) {
    printf("MB%02d Q0 %ld %d %f " SCANNAME "\n", (n+1), tweetids[*min_val], rank, *min_key);
    rank--;
  }
  
  heap_destroy(&h);
  free(args);
}
Beispiel #30
0
static void init_task_gen(struct task_base *tbase, struct task_args *targ)
{
	struct task_gen_server *task = (struct task_gen_server *)tbase;
	const int socket_id = rte_lcore_to_socket_id(targ->lconf->id);

	static char name[] = "server_mempool";
	name[0]++;
	task->mempool = rte_mempool_create(name,
					   4*1024 - 1, MBUF_SIZE,
					   targ->nb_cache_mbuf,
					   sizeof(struct rte_pktmbuf_pool_private),
					   rte_pktmbuf_pool_init, NULL,
					   rte_pktmbuf_init, 0,
					   socket_id, 0);
	PROX_PANIC(task->mempool == NULL, "Failed to allocate memory pool with %u elements\n", 4*1024 - 1);
	int pop = lua_getfrom(prox_lua(), GLOBAL, targ->streams);
	PROX_PANIC(pop < 0, "Failed to find '%s' in lua\n", targ->streams);

	lua_len(prox_lua(), -1);
	uint32_t n_listen = lua_tointeger(prox_lua(), -1);
	lua_pop(prox_lua(), 1);
	PROX_PANIC(n_listen == 0, "No services specified to listen on\n");

	task->bundle_cfgs = prox_zmalloc(n_listen * sizeof(task->bundle_cfgs[0]), socket_id);

	plogx_info("n_listen = %d\n", n_listen);

	struct hash_set *hs = prox_sh_find_socket(socket_id, "genl4_streams");
	if (hs == NULL) {
		/* Expected number of streams per bundle = 1, hash_set
		   will grow if full. */
		hs = hash_set_create(n_listen, socket_id);
		prox_sh_add_socket(socket_id, "genl4_streams", hs);
	}

	const struct rte_hash_parameters listen_table = {
		.name = name,
		.entries = n_listen * 4,
		.key_len = sizeof(struct new_tuple),
		.hash_func = rte_hash_crc,
		.hash_func_init_val = 0,
		.socket_id = socket_id,
	};
	name[0]++;

	task->listen_hash = rte_hash_create(&listen_table);
	task->listen_entries = prox_zmalloc(listen_table.entries * sizeof(task->listen_entries[0]), socket_id);

	int idx = 0;
	lua_pushnil(prox_lua());
	while (lua_next(prox_lua(), -2)) {
		task->bundle_cfgs[idx].n_stream_cfgs = 1;
		task->bundle_cfgs[idx].stream_cfgs = prox_zmalloc(sizeof(*task->bundle_cfgs[idx].stream_cfgs), socket_id);
		int ret = lua_to_stream_cfg(prox_lua(), STACK, NULL, socket_id, &task->bundle_cfgs[idx].stream_cfgs[0], hs);
		PROX_PANIC(ret, "Failed to load stream cfg\n");
		struct stream_cfg *stream = task->bundle_cfgs[idx].stream_cfgs[0];

		// TODO: check mask and add to hash for each host
		struct new_tuple nt = {
			.dst_addr = stream->servers.ip,
			.proto_id = stream->proto,
			.dst_port = stream->servers.port,
			.l2_types[0] = 0x0008,
		};

		ret = rte_hash_add_key(task->listen_hash, &nt);
		PROX_PANIC(ret < 0, "Failed to add\n");

		task->listen_entries[ret] = &task->bundle_cfgs[idx];

		plogx_dbg("Server = "IPv4_BYTES_FMT":%d\n", IPv4_BYTES(((uint8_t*)&nt.dst_addr)), rte_bswap16(nt.dst_port));
		++idx;
		lua_pop(prox_lua(), 1);
	}

	static char name2[] = "task_gen_hash2";

	name2[0]++;
	plogx_dbg("Creating bundle ctx pool\n");
	if (bundle_ctx_pool_create(name2, targ->n_concur_conn * 2, &task->bundle_ctx_pool, NULL, 0, NULL, socket_id)) {
		cmd_mem_stats();
		PROX_PANIC(1, "Failed to create conn_ctx_pool\n");
	}

	task->heap = heap_create(targ->n_concur_conn * 2, socket_id);
	task->seed = rte_rdtsc();

	/* TODO: calculate the CDF of the reply distribution and the
	   number of replies as the number to cover for 99% of the
	   replies. For now, assume that this is number is 2. */
	uint32_t queue_size = rte_align32pow2(targ->n_concur_conn * 2);

	PROX_PANIC(queue_size == 0, "Overflow resulted in queue size 0\n");
	task->fqueue = fqueue_create(queue_size, socket_id);
	PROX_PANIC(task->fqueue == NULL, "Failed to allocate local queue\n");

	uint32_t n_descriptors;

	if (targ->nb_txports) {
		PROX_PANIC(targ->nb_txports != 1, "Need exactly one TX port for L4 generation\n");
		n_descriptors = prox_port_cfg[targ->tx_port_queue[0].port].n_txd;
	} else {
		PROX_PANIC(targ->nb_txrings != 1, "Need exactly one TX ring for L4 generation\n");
		n_descriptors = 256;
	}

	struct token_time_cfg tt_cfg = {
		.bpp = targ->rate_bps,
		.period = rte_get_tsc_hz(),
		.bytes_max = n_descriptors * (ETHER_MIN_LEN + 20),
	};

	token_time_init(&task->token_time, &tt_cfg);
}

static void init_task_gen_client(struct task_base *tbase, struct task_args *targ)
{
	struct task_gen_client *task = (struct task_gen_client *)tbase;
	static char name[] = "gen_pool";
	const uint32_t socket = rte_lcore_to_socket_id(targ->lconf->id);
	name[0]++;
	task->mempool = rte_mempool_create(name,
					   4*1024 - 1, MBUF_SIZE,
					   targ->nb_cache_mbuf,
					   sizeof(struct rte_pktmbuf_pool_private),
					   rte_pktmbuf_pool_init, NULL,
					   rte_pktmbuf_init, 0,
					   socket, 0);
	PROX_PANIC(task->mempool == NULL, "Failed to allocate memory pool with %u elements\n", 4*1024 - 1);

	/* streams contains a lua table. Go through it and read each
	   stream with associated imix_fraction. */
	uint32_t imix;
	uint32_t i = 0;

	int pop = lua_getfrom(prox_lua(), GLOBAL, targ->streams);
	PROX_PANIC(pop < 0, "Failed to find '%s' in lua\n", targ->streams);

	lua_len(prox_lua(), -1);
	uint32_t n_bundle_cfgs = lua_tointeger(prox_lua(), -1);
	lua_pop(prox_lua(), 1);
	PROX_PANIC(n_bundle_cfgs == 0, "No configs specified\n");
	plogx_info("loading %d bundle_cfgs\n", n_bundle_cfgs);

	struct hash_set *hs = prox_sh_find_socket(socket, "genl4_streams");
	if (hs == NULL) {
		/* Expected number of streams per bundle = 8, hash_set
		   will grow if full. */
		hs = hash_set_create(n_bundle_cfgs * 8, socket);
		prox_sh_add_socket(socket, "genl4_streams", hs);
	}

	task->bundle_cfgs = prox_zmalloc(n_bundle_cfgs * sizeof(task->bundle_cfgs[0]), socket);
	lua_pushnil(prox_lua());

	int total_imix = 0;

	uint32_t *occur = prox_zmalloc(n_bundle_cfgs * sizeof(*occur), socket);
	struct cdf *cdf = cdf_create(n_bundle_cfgs, socket);

	while (lua_next(prox_lua(), -2)) {
		PROX_PANIC(lua_to_int(prox_lua(), TABLE, "imix_fraction", &imix) ||
			   lua_to_bundle_cfg(prox_lua(), TABLE, "bundle", socket, &task->bundle_cfgs[i], hs),
			   "Failed to load bundle cfg:\n%s\n", get_lua_to_errors());
		cdf_add(cdf, imix);
		occur[i] = imix;
		total_imix += imix;
		++i;
		lua_pop(prox_lua(), 1);
	}

	lua_pop(prox_lua(), pop);
	cdf_setup(cdf);

	PROX_PANIC(targ->max_setup_rate == 0, "Max setup rate not set\n");

	task->new_conn_cost = rte_get_tsc_hz()/targ->max_setup_rate;

	static char name2[] = "task_gen_hash";
	name2[0]++;
	plogx_dbg("Creating bundle ctx pool\n");
	if (bundle_ctx_pool_create(name2, targ->n_concur_conn, &task->bundle_ctx_pool, occur, n_bundle_cfgs, task->bundle_cfgs, socket)) {
		cmd_mem_stats();
		PROX_PANIC(1, "Failed to create conn_ctx_pool\n");
	}

	task->heap = heap_create(targ->n_concur_conn, socket);
	task->seed = rte_rdtsc();
	/* task->token_time.bytes_max = MAX_PKT_BURST * (ETHER_MAX_LEN + 20); */

	/* To avoid overflowing the tx descriptors, the token bucket
	   size needs to be limited. The descriptors are filled most
	   quickly with the smallest packets. For that reason, the
	   token bucket size is given by "number of tx descriptors" *
	   "smallest Ethernet packet". */
	PROX_ASSERT(targ->nb_txports == 1);

	struct token_time_cfg tt_cfg = {
		.bpp = targ->rate_bps,
		.period = rte_get_tsc_hz(),
		.bytes_max = prox_port_cfg[targ->tx_port_queue[0].port].n_txd * (ETHER_MIN_LEN + 20),
	};

	token_time_init(&task->token_time, &tt_cfg);
}

static void start_task_gen_client(struct task_base *tbase)
{
	struct task_gen_client *task = (struct task_gen_client *)tbase;

	token_time_reset(&task->token_time, rte_rdtsc(), 0);

	task->new_conn_tokens = 0;
	task->new_conn_last_tsc = rte_rdtsc();
}

static void stop_task_gen_client(struct task_base *tbase)
{
	struct task_gen_client *task = (struct task_gen_client *)tbase;
	struct bundle_ctx *bundle;

	while (!heap_is_empty(task->heap)) {
		bundle = BUNDLE_CTX_UPCAST(heap_pop(task->heap));
		bundle_expire(bundle, &task->bundle_ctx_pool, &task->l4_stats);
	}
}

static void start_task_gen_server(struct task_base *tbase)
{
	struct task_gen_server *task = (struct task_gen_server *)tbase;

	token_time_reset(&task->token_time, rte_rdtsc(), 0);
}

static void stop_task_gen_server(struct task_base *tbase)
{
	struct task_gen_server *task = (struct task_gen_server *)tbase;
	struct bundle_ctx *bundle;
	uint8_t out[MAX_PKT_BURST];

	while (!heap_is_empty(task->heap)) {
		bundle = BUNDLE_CTX_UPCAST(heap_pop(task->heap));
		bundle_expire(bundle, &task->bundle_ctx_pool, &task->l4_stats);
	}

	if (task->cancelled) {
		struct rte_mbuf *mbuf = task->mbuf_saved;

		out[0] = OUT_DISCARD;
		task->cancelled = 0;
		task->base.tx_pkt(&task->base, &mbuf, 1, out);
	}

	do {
		if (task->cur_mbufs_beg == task->cur_mbufs_end) {
			task->cur_mbufs_end = fqueue_get(task->fqueue, task->cur_mbufs, MAX_PKT_BURST);
			task->cur_mbufs_beg = 0;
			if (task->cur_mbufs_end == 0)
				break;
		}
		uint16_t n_pkts = task->cur_mbufs_end - task->cur_mbufs_beg;
		struct rte_mbuf **mbufs = task->cur_mbufs + task->cur_mbufs_beg;

		if (n_pkts) {
			for (uint16_t j = 0; j < n_pkts; ++j) {
				out[j] = OUT_DISCARD;
			}
			task->base.tx_pkt(&task->base, mbufs, n_pkts, out);
		}
	} while (1);
}

static struct task_init task_init_gen1 = {
	.mode_str = "genl4",
	.sub_mode_str = "server",
	.init = init_task_gen,
	.handle = handle_gen_bulk,
	.start = start_task_gen_server,
	.stop = stop_task_gen_server,
	.flag_features = TASK_FEATURE_ZERO_RX,
	.size = sizeof(struct task_gen_server),
	.mbuf_size = 2048 + sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM,
};

static struct task_init task_init_gen2 = {
	.mode_str = "genl4",
	.init = init_task_gen_client,
	.handle = handle_gen_bulk_client,
	.start = start_task_gen_client,
	.stop = stop_task_gen_client,
	.flag_features = TASK_FEATURE_ZERO_RX,
	.size = sizeof(struct task_gen_client),
	.mbuf_size = 2048 + sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM,
};

__attribute__((constructor)) static void reg_task_gen(void)
{
	reg_task(&task_init_gen1);
	reg_task(&task_init_gen2);
}