Coverage Report

Created: 2019-07-03 22:50

/home/liu/buildslave/linux-x64-runtests/build/test/unit/memory.tests.c
Line
Count
Source (jump to first uncovered line)
1
#include "ccv.h"
2
#include "ccv_internal.h"
3
#include "case.h"
4
5
uint64_t uniqid()
6
1.00M
{
7
1.00M
  union {
8
1.00M
    uint64_t u;
9
1.00M
    uint8_t chr[8];
10
1.00M
  } sign;
11
1.00M
  int i;
12
9.00M
  for (i = 0; i < 8; 
i++8.00M
)
13
8.00M
    sign.chr[i] = rand() & 0xff;
14
1.00M
  return sign.u;
15
1.00M
}
16
17
10.8M
#define N (1000000)
18
19
TEST_CASE("random cache put/delete/get")
20
1
{
21
1
  ccv_cache_t cache;
22
1
  ccv_cache_init(&cache, N, 1, ccfree);
23
1
  uint64_t* sigs = ccmalloc(sizeof(uint64_t) * N);
24
1
  void** mems = ccmalloc(sizeof(void*) * N);
25
1
  int i;
26
1.00M
  for (i = 0; i < N; 
i++1.00M
)
27
1.00M
  {
28
1.00M
    sigs[i] = uniqid();
29
1.00M
    mems[i] = ccmalloc(1);
30
1.00M
    ccv_cache_put(&cache, sigs[i], mems[i], 1, 0);
31
1.00M
    REQUIRE_EQ(i + 1, cache.size, "at %d should has cache size %d", i, i);
32
1.00M
  }
33
1
  uint8_t deleted[N];
34
1.00M
  for (i = 0; i < N; 
i++1.00M
)
35
1.00M
  {
36
1.00M
    deleted[i] = 1;
37
1.00M
    if (deleted[i])
38
1.00M
      ccv_cache_delete(&cache, sigs[i]);
39
1.00M
    REQUIRE_EQ(N - 1 - i, cache.size, "at %d should has cache size %d", 
i, 0
N0
- 1 - i);
40
1.00M
  }
41
1.00M
  
for (i = 0; 1
i < N;
i++1.00M
)
42
1.00M
  {
43
1.00M
    deleted[i] = (rand() % 3 == 0);
44
1.00M
    if (!deleted[i])
45
665k
    {
46
665k
      mems[i] = ccmalloc(1);
47
665k
      ccv_cache_put(&cache, sigs[i], mems[i], 1, 0);
48
665k
    }
49
1.00M
  }
50
1.00M
  for (i = 0; i < N; 
i++1.00M
)
51
1.00M
  {
52
1.00M
    deleted[i] = (rand() % 3 == 0);
53
1.00M
    if (deleted[i])
54
333k
      ccv_cache_delete(&cache, sigs[i]);
55
666k
    else {
56
666k
      mems[i] = ccmalloc(1);
57
666k
      ccv_cache_put(&cache, sigs[i], mems[i], 1, 0);
58
666k
    }
59
1.00M
  }
60
1.00M
  for (i = 0; i < N; 
i++1.00M
)
61
1.00M
  {
62
1.00M
    void* x = ccv_cache_get(&cache, sigs[i], 0);
63
1.00M
    if (!deleted[i] && 
x666k
) // x may be pull off the cache
64
666k
    {
65
666k
      REQUIRE_EQ((uint64_t)mems[i], (uint64_t)x, "value at %d should be consistent", i);
66
666k
    } else
67
1.00M
      
REQUIRE_EQ333k
(0, (uint64_t)x, "at %d should not exist", i);
68
1.00M
  }
69
1
  ccv_cache_close(&cache);
70
1
  ccfree(mems);
71
1
  ccfree(sigs);
72
1
}
73
74
TEST_CASE("garbage collector 95\% hit rate")
75
1
{
76
1
  int i;
77
1
  // deliberately let only cache size fits 90% of data
78
1
  ccv_enable_cache(ccv_compute_dense_matrix_size(1, 1, CCV_32S | CCV_C1) * N * 9 / 10);
79
1.00M
  for (i = 0; i < N; 
i++1.00M
)
80
1.00M
  {
81
1.00M
    ccv_dense_matrix_t* dmt = ccv_dense_matrix_new(1, 1, CCV_32S | CCV_C1, 0, 0);
82
1.00M
    dmt->data.i32[0] = i;
83
1.00M
    dmt->sig = ccv_cache_generate_signature((const char*)&i, 4, CCV_EOF_SIGN);
84
1.00M
    dmt->type |= CCV_REUSABLE;
85
1.00M
    ccv_matrix_free(dmt);
86
1.00M
  }
87
1
  int percent = 0, total = 0;
88
940k
  for (i = 
N1
- 1; i > N * 6 / 100;
i--939k
)
89
939k
  {
90
939k
    uint64_t sig = ccv_cache_generate_signature((const char*)&i, 4, CCV_EOF_SIGN);
91
939k
    ccv_dense_matrix_t* dmt = ccv_dense_matrix_new(1, 1, CCV_32S | CCV_C1, 0, sig);
92
939k
    if (i == dmt->data.i32[0])
93
900k
      ++percent;
94
939k
    ++total;
95
939k
    ccv_matrix_free_immediately(dmt);
96
939k
  }
97
1
  REQUIRE((double)percent / (double)total > 0.95, "the cache hit (%lf) should be greater than 95%%", (double)percent / (double)total);
98
1
  ccv_disable_cache();
99
1
}
100
101
TEST_CASE("garbage collector 47\% hit rate")
102
1
{
103
1
  int i;
104
1
  // deliberately let only cache size fits 90% of data
105
1
  ccv_enable_cache(ccv_compute_dense_matrix_size(1, 1, CCV_32S | CCV_C1) * N * 45 / 100);
106
1.00M
  for (i = 0; i < N; 
i++1.00M
)
107
1.00M
  {
108
1.00M
    ccv_dense_matrix_t* dmt = ccv_dense_matrix_new(1, 1, CCV_32S | CCV_C1, 0, 0);
109
1.00M
    dmt->data.i32[0] = i;
110
1.00M
    dmt->sig = ccv_cache_generate_signature((const char*)&i, 4, CCV_EOF_SIGN);
111
1.00M
    dmt->type |= CCV_REUSABLE;
112
1.00M
    ccv_matrix_free(dmt);
113
1.00M
  }
114
1
  int percent = 0, total = 0;
115
940k
  for (i = 
N1
- 1; i > N * 6 / 100;
i--939k
)
116
939k
  {
117
939k
    uint64_t sig = ccv_cache_generate_signature((const char*)&i, 4, CCV_EOF_SIGN);
118
939k
    ccv_dense_matrix_t* dmt = ccv_dense_matrix_new(1, 1, CCV_32S | CCV_C1, 0, sig);
119
939k
    if (i == dmt->data.i32[0])
120
450k
      ++percent;
121
939k
    ++total;
122
939k
    ccv_matrix_free_immediately(dmt);
123
939k
  }
124
1
  REQUIRE((double)percent / (double)total > 0.47, "the cache hit (%lf) should be greater than 47%%", (double)percent / (double)total);
125
1
  ccv_disable_cache();
126
1
}
127
128
TEST_CASE("multi-type garbage collector 92\% hit rate")
129
1
{
130
1
  int i;
131
1
  ccv_enable_cache((ccv_compute_dense_matrix_size(1, 1, CCV_32S | CCV_C1) + (sizeof(ccv_array_t) + 4 * 4)) * N * 9 / 10);
132
1.00M
  for (i = 0; i < N; 
i++1.00M
)
133
1.00M
  {
134
1.00M
    ccv_dense_matrix_t* dmt = ccv_dense_matrix_new(1, 1, CCV_32S | CCV_C1, 0, 0);
135
1.00M
    dmt->data.i32[0] = i;
136
1.00M
    dmt->sig = ccv_cache_generate_signature((const char*)&i, 4, CCV_EOF_SIGN);
137
1.00M
    dmt->type |= CCV_REUSABLE;
138
1.00M
    ccv_matrix_free(dmt);
139
1.00M
    ccv_array_t* array = ccv_array_new(4, 4, 0);
140
1.00M
    int j = i;
141
1.00M
    ccv_array_push(array, &j);
142
1.00M
    j = 0;
143
1.00M
    ccv_array_push(array, &j);
144
1.00M
    j = ~i;
145
1.00M
    ccv_array_push(array, &j);
146
1.00M
    j = -i;
147
1.00M
    ccv_array_push(array, &j);
148
1.00M
    array->sig = ccv_cache_generate_signature((const char*)&j, 4, CCV_EOF_SIGN);
149
1.00M
    array->type |= CCV_REUSABLE;
150
1.00M
    ccv_array_free(array);
151
1.00M
  }
152
1
  int percent = 0, total = 0;
153
940k
  for (i = 
N1
- 1; i > N * 6 / 100;
i--939k
)
154
939k
  {
155
939k
    uint64_t sig = ccv_cache_generate_signature((const char*)&i, 4, CCV_EOF_SIGN);
156
939k
    ccv_dense_matrix_t* dmt = ccv_dense_matrix_new(1, 1, CCV_32S | CCV_C1, 0, sig);
157
939k
    if (i == dmt->data.i32[0])
158
900k
      ++percent;
159
939k
    ++total;
160
939k
    ccv_matrix_free_immediately(dmt);
161
939k
    int j = -i;
162
939k
    sig = ccv_cache_generate_signature((const char*)&j, 4, CCV_EOF_SIGN);
163
939k
    ccv_array_t* array = ccv_array_new(4, 4, sig);
164
939k
    if (i == *(int*)ccv_array_get(array, 0) &&
165
939k
      
0 == *(int*)900k
ccv_array_get900k
(array, 1) &&
166
939k
      
~i == *(int*)900k
ccv_array_get900k
(array, 2) &&
167
939k
      
-i == *(int*)900k
ccv_array_get900k
(array, 3))
168
939k
      
++percent900k
;
169
939k
    ++total;
170
939k
    ccv_array_free_immediately(array);
171
939k
  }
172
1
  REQUIRE((double)percent / (double)total > 0.92, "the cache hit (%lf) should be greater than 92%%", (double)percent / (double)total);
173
1
  ccv_disable_cache();
174
1
}
175
176
#include "case_main.h"