Allow latency thresholds for TST009 testing
[samplevnf.git] / VNFs / DPPD-PROX / cdf.c
1 /*
2 // Copyright (c) 2010-2017 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16
17 #include <stdlib.h>
18 #include <inttypes.h>
19
20 #include <rte_cycles.h>
21
22 #include "prox_malloc.h"
23 #include "cdf.h"
24
25 static uint32_t round_pow2(uint32_t val)
26 {
27         uint32_t ret;
28         uint32_t s = 1 << 31;
29
30         while ((s & val) == 0)
31                 s = s >> 1;
32         if (s == 1U << 31 && s != val)
33                 return 0;
34
35         ret = val;
36         if (s != ret)
37                 ret = (s << 1);
38
39         return ret;
40 }
41
42 static uint32_t get_r_max(struct cdf *cdf, uint32_t cur)
43 {
44         uint32_t right_child = cur;
45
46         do {
47                 cur = right_child;
48                 right_child = cur * 2 + 1;
49         } while (right_child < cdf->elems[0]);
50
51         return cdf->elems[cur];
52 }
53
54 struct cdf *cdf_create(uint32_t n_vals, int socket_id)
55 {
56         struct cdf *ret;
57         size_t mem_size = 0;
58         uint32_t n_vals_round = round_pow2(n_vals);
59
60         if (0 == n_vals_round)
61                 return NULL;
62
63         mem_size += sizeof(struct cdf);
64         mem_size += sizeof(((struct cdf *)(0))->elems[0]) * n_vals_round * 2;
65         ret = prox_zmalloc(mem_size, socket_id);
66         ret->elems[0] = n_vals;
67
68         /* leafs are [n_vals, 2 * n_vals[. During cdf_add() and
69            cdf_setup(), rand_max refers to the index of the next leaf
70            to be added.  */
71         ret->rand_max = n_vals_round;
72         ret->first_child = n_vals_round;
73         ret->seed = rte_rdtsc();
74
75         return ret;
76 }
77
78 void cdf_add(struct cdf *cdf, uint32_t len)
79 {
80         cdf->elems[cdf->rand_max++] = len;
81 }
82
83 int cdf_setup(struct cdf *cdf)
84 {
85         uint32_t last_leaf, first_leaf;
86         uint32_t first_parent, last_parent;
87         uint32_t total, multiplier, cur, end;
88
89         if (cdf->elems[0] == 1) {
90                 cdf->rand_max = RAND_MAX;
91                 cdf->elems[1] = RAND_MAX;
92                 cdf->elems[0] = 2;
93                 return 0;
94         }
95
96         last_leaf  = cdf->rand_max;
97         first_leaf = round_pow2(cdf->elems[0]);
98         /* Failed to add all elements through cdf_add() */
99         if (last_leaf - first_leaf != cdf->elems[0])
100                 return -1;
101
102         total = 0;
103         for (uint32_t i = first_leaf; i < last_leaf; ++i) {
104                 total += cdf->elems[i];
105         }
106
107         multiplier = RAND_MAX / total;
108         if (multiplier * total == RAND_MAX)
109                 multiplier--;
110         cdf->rand_max = multiplier * total;
111         total = 0;
112         for (uint32_t i = first_leaf; i < last_leaf; ++i) {
113                 uint32_t cur = cdf->elems[i];
114
115                 /* Each element represents the range between previous
116                    total (non-inclusive) and new total (inclusive). */
117                 total += cur * multiplier - 1;
118                 cdf->elems[i] = total;
119                 total += 1;
120         }
121         end = round_pow2(first_leaf) << 1;
122         for (uint32_t i = last_leaf; i < end; ++i) {
123                 cdf->elems[i] = RAND_MAX;
124         }
125         cdf->first_child = first_leaf;
126         cdf->elems[0] = end;
127
128         /* Build the binary tree used at run-time. */
129         last_leaf = end - 1;
130         do {
131                 first_parent = first_leaf/2;
132                 last_parent  = last_leaf/2;
133
134                 for (uint32_t i = first_parent; i <= last_parent; ++i) {
135                         /* The current nodes value should be the
136                            biggest value accessible through its left
137                            child. This value is stored in the right
138                            most child of the left child. The left most
139                            child of the right child is the first value
140                            that can not be accessed through the left
141                            child.  */
142                         cdf->elems[i] = get_r_max(cdf, i * 2);
143                 }
144                 first_leaf = first_parent;
145                 last_leaf = last_parent;
146         } while (first_parent != last_parent);
147         return 0;
148 }