2 gcc -fPIC -c get_rate.c -I$( pg_config --includedir-server )
3 gcc -shared -o get_rate.so get_rate.o
10 #include "catalog/pg_type.h"
11 #include "utils/lsyscache.h"
12 #include "utils/array.h"
13 #include "access/heapam.h"
15 #ifdef PG_MODULE_MAGIC
19 PG_FUNCTION_INFO_V1(get_rate);
22 get_rate(PG_FUNCTION_ARGS)
24 float8 bytes_in_1 = PG_GETARG_FLOAT8(0);
25 float8 bytes_out_1 = PG_GETARG_FLOAT8(1);
26 float8 time_1 = PG_GETARG_FLOAT8(2);
28 float8 bytes_in_2 = PG_GETARG_FLOAT8(3);
29 float8 bytes_out_2 = PG_GETARG_FLOAT8(4);
30 float8 time_2 = PG_GETARG_FLOAT8(5);
32 bool no_wrap = PG_GETARG_BOOL(6);
40 double in_rate, out_rate;
42 if (bytes_in_2 >= bytes_in_1 && bytes_out_2 >= bytes_out_1) {
43 // regular case, no wraparound or reset
44 in_rate = (bytes_in_2 - bytes_in_1) / (time_2 - time_1);
45 out_rate = (bytes_out_2 - bytes_out_1) / (time_2 - time_1);
47 // is wraparound unlikely?
49 || (bytes_in_2 + 4294967296.0 - bytes_in_1) >= 2147483648.0
50 || (bytes_out_2 + 4294967296.0 - bytes_out_1) >= 2147483648.0) {
51 // assume the switch zeroed in the middle of the poll interval
52 in_rate = bytes_in_2 / (2.0 * (time_2 - time_1));
53 out_rate = bytes_out_2 / (2.0 * (time_2 - time_1));
55 // probably wraparound
56 if (bytes_in_2 < bytes_in_1)
57 bytes_in_2 += 4294967296.0;
58 if (bytes_out_2 < bytes_out_1)
59 bytes_out_2 += 4294967296.0;
61 in_rate = (bytes_in_2 - bytes_in_1) / (time_2 - time_1);
62 out_rate = (bytes_out_2 - bytes_out_1) / (time_2 - time_1);
66 values[0] = Float8GetDatum(in_rate);
67 values[1] = Float8GetDatum(out_rate);
68 get_typlenbyvalalign(FLOAT8OID, &elmlen, &elmbyval, &elmalign);
69 PG_RETURN_ARRAYTYPE_P(construct_array(values, 2, FLOAT8OID, elmlen, elmbyval, elmalign));