Merge from vendor branch GCC:
[dragonfly.git] / lib / libkvm / kvm_util.c
1 /*
2  * Copyright (c) 2003-2004.  Hiten Pandya <hmp@backplane.com>.
3  * 
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following
12  *    disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  *       
18  * THIS SOFTWARE IS PROVIDED BY HITEN PANDYA AND CONTRIBUTORS "AS IS" AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL HITEN PANDYA OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
25  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * $DragonFly: src/lib/libkvm/kvm_util.c,v 1.1 2004/04/02 05:46:02 hmp Exp $
31  */
32
33 /*
34  * Useful functions that are used across the source base for
35  * various purpose.
36  */
37
38 #include <sys/param.h>
39 #include <sys/nchstats.h>
40 #include <sys/sysctl.h>
41
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <strings.h>
45 #include <err.h>
46
47 /*
48  * Aggregate the per-cpu counters we retrieved via sysctl(2)
49  * to give the total across the CPUs.  Use a nasty trick to
50  * aggregate the counters in the structure! YYY
51  */
52 void
53 kvm_nch_cpuagg(struct nchstats *unagg, struct nchstats *ttl, int cpucnt)
54 {
55         int i, off, siz;
56         siz = sizeof(struct nchstats);
57
58         if (!unagg && !ttl)
59                 return;
60
61         bzero(ttl, siz);
62         
63         /* kick hmp@ for this nasty loop! :-) */
64         for (i = 0; i < cpucnt; ++i) {
65                 for (off = 0; off < siz; off += sizeof(u_long)) {
66                         *(u_long *)((char *)(*(&ttl)) + off) +=
67                         *(u_long *)((char *)&unagg[i] + off);
68                 }
69         }
70 }