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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
/*
* Copyright (C) 2000-2007 SWsoft. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <ctype.h>
#include "ub.h"
#include "config.h"
#include "util.h"
void usage(int rc)
{
fprintf(rc ? stderr : stdout, "Usage: vzcalc [-v] <veid>\n");
exit(rc);
}
static int check_param(struct ub_struct *param)
{
int ret = 0;
#define CHECKPARAM(name, str_name) \
if (param->name == NULL) { \
fprintf(stderr, "Error: parameter " #str_name " not found\n"); \
ret = 1; \
}
CHECKPARAM(numproc, NUMPROC);
CHECKPARAM(vmguarpages, VMGUARPAGES);
CHECKPARAM(kmemsize, KMEMSIZE);
CHECKPARAM(tcpsndbuf, TCPSNDBUF);
CHECKPARAM(tcprcvbuf, TCPRCVBUF);
CHECKPARAM(othersockbuf, OTHERSOCKBUF);
CHECKPARAM(dgramrcvbuf, DGRAMRCVBUF);
CHECKPARAM(privvmpages, PRIVVMPAGES);
return ret;
}
int calculate(int veid, ub_param *ub, int verbose)
{
ub_param ub_cur;
char tmp[STR_SIZE];
int ret = 0;
double kmem_net_cur, lm_cur, tr_cur, ms_cur, al_cur, np_cur;
double kmem_net_max, lm_max, lm_prm, ms_prm, al_prm, al_max, np_max;
double cur, prm, max;
unsigned long long lowmem;
unsigned long long ram, swap = 1;
int run, thrmax;
int page;
if (check_param(ub))
return 1;
if (get_mem(&ram))
return 1;
if (get_thrmax(&thrmax))
return 1;
get_swap(&swap);
if (get_lowmem(&lowmem))
return 1;
page = get_pagesize();
lm_cur = tr_cur = ms_cur = al_cur = np_cur = 0;
lowmem *= 0.4;
memset(&ub_cur, 0, sizeof(ub_cur));
if (!(run = vps_read_ubc(veid, &ub_cur))) {
if (check_param(&ub_cur))
return 1;
kmem_net_cur = (double)ub_cur.kmemsize[0] +
(double)ub_cur.tcprcvbuf[0] +
(double)ub_cur.tcpsndbuf[0] +
(double)ub_cur.dgramrcvbuf[0] +
(double)ub_cur.othersockbuf[0];
/* Low memory */
lm_cur = kmem_net_cur / lowmem;
/* Total RAM */
tr_cur = ((double)ub_cur.physpages[0] * page + kmem_net_cur)
/ ram;
/* Mem + Swap */
ms_cur = ((double)ub_cur.oomguarpages[0] * page +
kmem_net_cur) / (ram + swap);
/* Allocated */
al_cur = ((double)ub_cur.privvmpages[0] * page +
kmem_net_cur) / (ram + swap);
/* Numproc */
np_cur = (double)ub_cur.numproc[0] / thrmax;
}
kmem_net_max = (double)ub->kmemsize[1] +
(double)ub->tcprcvbuf[1] +
(double)ub->tcpsndbuf[1] +
(double)ub->dgramrcvbuf[1] +
(double)ub->othersockbuf[1];
/* Low memory */
lm_max = kmem_net_max / lowmem;
lm_prm = lm_max;
/* Mem + Swap */
ms_prm = ((double)ub->oomguarpages[0] * page + kmem_net_max) /
(ram + swap);
/* Allocated */
al_prm = ((double)ub->vmguarpages[0] * page + kmem_net_max) /
(ram + swap);
al_max = ((double)ub->privvmpages[1] * page + kmem_net_max) /
(ram + swap);
/* Numproc */
np_max = (double)ub->numproc[0] / thrmax;
/* Calculate maximum for current */
cur = lm_cur;
if (tr_cur > cur)
cur = tr_cur;
if (ms_cur > cur)
cur = ms_cur;
if (al_cur > cur)
cur = al_cur;
if (np_cur > cur)
cur = np_cur;
/* Calculate maximum for promised */
prm = lm_prm;
if (ms_prm > prm)
prm = ms_prm;
if (al_prm > prm)
prm = al_prm;
/* Calculate maximum for Max */
max = lm_max;
if (al_max > max)
max = al_max;
if (np_max > max)
max = np_max;
sprintf(tmp, "n/a");
printf("Resource Current(%%) Promised(%%) Max(%%)\n");
if (verbose) {
if (!run)
sprintf(tmp, "%10.2f", lm_cur * 100);
printf("Low Mem %10s %10.2f %10.2f\n",
tmp, lm_prm * 100, lm_max * 100);
if (!run)
sprintf(tmp, "%10.2f", tr_cur * 100);
printf("Total RAM %10s n/a n/a \n", tmp);
if (!run)
sprintf(tmp, "%10.2f", ms_cur * 100);
printf("Mem + Swap %10s %10.2f n/a\n",
tmp, ms_prm * 100);
if (!run)
sprintf(tmp, "%10.2f", al_cur * 100);
printf("Alloc. Mem %10s %10.2f %10.2f\n",
tmp , al_prm * 100, al_max * 100);
if (!run)
sprintf(tmp, "%10.2f", np_cur * 100);
printf("Num. Proc %10s n/a %10.2f\n",
tmp, np_max * 100);
printf("--------------------------------------------\n");
}
if (!run)
sprintf(tmp, "%10.2f", cur * 100);
printf("Memory %10s %10.2f %10.2f\n", tmp, prm * 100, max * 100);
free_ub_param(&ub_cur);
return ret;
}
int main(int argc, char **argv)
{
struct stat st;
char path[64];
int ret, opt, veid, verbose = 0;
vps_param *param;
while ((opt = getopt(argc, argv, "vh")) > 0) {
switch(opt) {
case 'v':
verbose = 1;
break;
case 'h':
usage(0);
break;
default :
exit(1);
}
}
if (optind == argc)
usage(1);
if (parse_int(argv[optind], &veid)) {
fprintf(stderr, "Invalid VE ID: %s\n", argv[optind]);
exit(1);
}
get_vps_conf_path(veid, path, sizeof(path));
if (stat(path, &st)) {
fprintf(stderr, "VE configuration file: %s not found\n", path);
exit(1);
}
param = init_vps_param();
if (vps_parse_config(veid, path, param, NULL))
exit(1);
ret = calculate(veid, ¶m->res.ub, verbose);
free_vps_param(param);
exit(ret);
}
|