summaryrefslogtreecommitdiff
path: root/qa/src/pmconvscale.c
blob: 5d03e38f9f07512531087be6d9c3ca4639023a89 (plain)
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
/*
 * pmconvscale - tests bug in pmConvScale
 *
 * Copyright (c) 1995-2001 Silicon Graphics, Inc.  All Rights Reserved.
 */

#include <unistd.h>
#include <pcp/pmapi.h>
#include <pcp/impl.h>

/* default is the "no" dimension case */
pmUnits		units = {0, 0, 0, 0, 0, 0};
pmUnits		oldunits = {0, 0, 0, 0, 0, 0};

static int sscales[] = { PM_SPACE_GBYTE, PM_SPACE_TBYTE, PM_SPACE_GBYTE, PM_SPACE_MBYTE, PM_SPACE_KBYTE, PM_SPACE_BYTE, PM_SPACE_KBYTE, PM_SPACE_MBYTE };
static int n_sscales = sizeof(sscales) / sizeof(sscales[0]);

static int tscales[] = { PM_TIME_MIN, PM_TIME_HOUR, PM_TIME_MIN, PM_TIME_SEC, PM_TIME_MSEC, PM_TIME_USEC, PM_TIME_NSEC, PM_TIME_USEC, PM_TIME_MSEC, PM_TIME_SEC };
static int n_tscales = sizeof(tscales) / sizeof(tscales[0]);

static int cscales[] = {1, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2, -3, -4, -5, -6, -7, -8, -7, -6, -5, -4, -3, -2, -1, 0 }; 
static int n_cscales = sizeof(cscales) / sizeof(cscales[0]);

/* just for PBYTE and EBYTE space extensions */
#ifdef HAVE_BITFIELDS_LTOR
pmUnits myunits = { 1, -1, 0, PM_SPACE_EBYTE, PM_TIME_SEC, 0 };
#else
pmUnits myunits = { 0, 0, PM_TIME_SEC, PM_SPACE_EBYTE, 0, -1, 1 };
#endif

int
main(int argc, char **argv)
{
    int i, sts;
    int	mode = 1;
    int	limit = 0;
    pmAtomValue value, newvalue;
    char olds[64], news[64];

    value.f = 12345678;

    if (argc > 1) {
	if (argv[1][0] == 'c') {
	    /* crude, but effective ... the "count" dimension case */
	    units.dimCount = 1;
	    mode = 1;
	}
	else if (argv[1][0] == 's') {
	    /* crude, but effective ... the "space" dimension case */
	    units.dimSpace = 1;
	    units.scaleSpace = PM_SPACE_MBYTE;
	    mode = 2;
	}
	else if (argv[1][0] == 'r') {
	    /* crude, but effective ... the "space/time" dimension case */
	    units.dimSpace = 1;
	    units.dimTime = -1;
	    units.scaleSpace = PM_SPACE_MBYTE;
	    units.scaleTime = PM_TIME_SEC;
	    mode = 2;
	}
	else if (argv[1][0] == 't') {
	    /* crude, but effective ... the "time" dimension case */
	    units.dimTime = 1;
	    units.scaleTime = PM_TIME_SEC;
	    mode = 3;
	}
	else if (argv[1][0] == 'x') {
	    /* PBYTE and EBYTE space extensions */
	    value.f = 1;
	    while (myunits.scaleSpace > 0) {
		units = myunits;
		units.scaleSpace--;
		sts = pmConvScale(PM_TYPE_FLOAT, &value, &myunits, &newvalue, &units);
		if (sts < 0) {
		    strcpy(olds, pmUnitsStr(&oldunits));
		    strcpy(news, pmUnitsStr(&units));
		    fprintf(stderr, "pmConvScale \"%s\" -> \"%s\" failed: %s\n", olds, news, pmErrStr(sts));
		    exit(1);
		}
		printf("%12.1f %s -> ", value.f, pmUnitsStr(&myunits));
		printf("%12.1f %s\n", newvalue.f, pmUnitsStr(&units));
		myunits.scaleSpace--;
		value.f = newvalue.f / 512;
	    }
	    value.f *= 512;
	    while (myunits.scaleSpace < PM_SPACE_EBYTE) {
		units = myunits;
		units.scaleSpace++;
		sts = pmConvScale(PM_TYPE_FLOAT, &value, &myunits, &newvalue, &units);
		if (sts < 0) {
		    strcpy(olds, pmUnitsStr(&oldunits));
		    strcpy(news, pmUnitsStr(&units));
		    fprintf(stderr, "pmConvScale \"%s\" -> \"%s\" failed: %s\n", olds, news, pmErrStr(sts));
		    exit(1);
		}
		printf("%12.1f %s -> ", value.f, pmUnitsStr(&myunits));
		printf("%12.1f %s\n", newvalue.f, pmUnitsStr(&units));
		myunits.scaleSpace++;
		value.f = newvalue.f * 512;
	    }
	    exit(0);
	}
    }

    oldunits = units;

    if (mode == 1)
	limit = n_cscales;
    else if (mode == 2)
	limit = n_sscales;
    else if (mode == 3)
	limit = n_tscales;

    for (i=0; i < limit; i++) {
	if (mode == 1)
	    units.scaleCount = cscales[i];
	else if (mode == 2)
	    units.scaleSpace = sscales[i];
	else if (mode == 3)
	    units.scaleTime = tscales[i];

	strcpy(olds, pmUnitsStr(&oldunits));
	strcpy(news, pmUnitsStr(&units));

	sts = pmConvScale(PM_TYPE_FLOAT, &value, &oldunits, &newvalue, &units);
	if (sts < 0) {
	    fprintf(stderr, "pmConvScale \"%s\" -> \"%s\" failed: %s\n", olds, news, pmErrStr(sts));
	    exit(1);
	}

	if (mode == 1)
	    printf("scaleCount=%d old: %12.1f \"%s\" new: %12.1f \"%s\"\n",
		units.scaleCount, value.f, olds, newvalue.f, news);
	else if (mode == 2)
	    printf("scaleSpace=%d old: %12.1f \"%s\" new: %12.1f \"%s\"\n",
		units.scaleSpace, value.f, olds, newvalue.f, news);
	else if (mode == 3)
	    printf("scaleTime=%d old: %12.1f \"%s\" new: %12.1f \"%s\"\n",
		units.scaleTime, value.f, olds, newvalue.f, news);

	value.f = newvalue.f;
	oldunits = units;
    }
    exit(0);
}