1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
/*
* This file and its contents are supplied under the terms of the
* Common Development and Distribution License ("CDDL"), version 1.0.
* You may only use this file in accordance with the terms of version
* 1.0 of the CDDL.
*
* A full copy of the text of the CDDL should have accompanied this
* source. A copy of the CDDL is also available via the Internet at
* http://www.illumos.org/license/CDDL.
*/
/*
* Copyright 2019 Joyent, Inc.
*/
#include <sys/cpu_uarray.h>
#include <sys/sysmacros.h>
#include <sys/cpuvar.h>
#include <sys/debug.h>
#include <sys/kmem.h>
static size_t
cpu_uarray_size(size_t nr_items)
{
size_t size = P2ROUNDUP(nr_items * sizeof (uint64_t), CUA_ALIGN);
size *= NCPU;
return (sizeof (cpu_uarray_t) + size);
}
cpu_uarray_t *
cpu_uarray_zalloc(size_t nr_items, int kmflags)
{
cpu_uarray_t *cua;
cua = kmem_zalloc(cpu_uarray_size(nr_items), kmflags);
if (cua != NULL) {
VERIFY(IS_P2ALIGNED(cua->cu_vals, CUA_ALIGN));
cua->cu_nr_items = nr_items;
}
return (cua);
}
void
cpu_uarray_free(cpu_uarray_t *cua)
{
if (cua != NULL)
kmem_free(cua, cpu_uarray_size(cua->cu_nr_items));
}
uint64_t
cpu_uarray_sum(cpu_uarray_t *cua, size_t index)
{
uint64_t sum = 0;
VERIFY3U(index, <, cua->cu_nr_items);
for (size_t c = 0; c < ncpus; c++) {
uint64_t addend = CPU_UARRAY_VAL(cua, c, index);
sum = UINT64_OVERFLOW_ADD(sum, addend);
}
return (sum);
}
uint64_t
cpu_uarray_sum_all(cpu_uarray_t *cua)
{
uint64_t sum = 0;
for (size_t c = 0; c < ncpus; c++) {
for (size_t i = 0; i < cua->cu_nr_items; i++) {
uint64_t addend = CPU_UARRAY_VAL(cua, c, i);
sum = UINT64_OVERFLOW_ADD(sum, addend);
}
}
return (sum);
}
|