-
Notifications
You must be signed in to change notification settings - Fork 0
/
ass2.c
176 lines (152 loc) · 4.49 KB
/
ass2.c
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
#include <stdio.h>
#include <stdlib.h>
#include "postgres.h"
#include <string.h>
#include "miscadmin.h"
#include "fmgr.h"
#include "funcapi.h"
#include <utils/builtins.h>
#define SIZE 5
#define CPU_VENDOR 0
#define MODEL_NAME 1
#define PHY_PROCESSOR 2
#define CORES 3
#define CLOCK_SPEED 4
PG_MODULE_MAGIC;
void read_cpu_info(Tuplestorestate *tupstore, TupleDesc tupdesc);
char *trimStr(char *s);
bool checkAvailability(char *line, char arr[]);
PG_FUNCTION_INFO_V1(ass2);
bool checkAvailability(char *line, char arr[])
{
char *result = strstr(line, arr);
return (result != NULL);
}
char *trimStr(char *s)
{
char *new_str = s;
while (isspace(*new_str))
{
++new_str;
}
memmove(s, new_str, strlen(new_str) + 1);
char *end = s + strlen(s);
while ((end != s) && isspace(*(end - 1)))
{
--end;
}
*end = '\0';
return s;
}
void read_cpu_info(Tuplestorestate *tupstore, TupleDesc tupdesc)
{
char *found;
FILE *file;
Datum values[SIZE];
bool nulls[SIZE];
char vendor_id[MAXPGPATH];
char model[MAXPGPATH];
char model_name[MAXPGPATH];
char cpu_mhz[MAXPGPATH];
char *line = NULL;
bool is_vendor = false;
bool is_model_name = false;
// bool is_cpuMhz = false;
bool is_cpu_cores = false;
size_t line_buf_size = 0;
ssize_t line_size;
bool model_found = false;
int physical_processor = 0;
int cpu_cores = 0;
float cpu_hz;
uint64_t cpu_freq;
memset(nulls, 0, sizeof(nulls));
memset(vendor_id, 0, MAXPGPATH);
memset(model, 0, MAXPGPATH);
memset(model_name, 0, MAXPGPATH);
memset(cpu_mhz, 0, MAXPGPATH);
file = fopen("/proc/cpuinfo", "r");
line_size = getline(&line, &line_buf_size, file);
while (line_size >= 0)
{
if (strlen(line) > 0)
line = trimStr(line);
if (strlen(line) > 0)
{
found = strstr(line, ":");
if (strlen(found) > 0)
{
found = trimStr((found + 1));
if (!is_vendor && checkAvailability(line, "vendor_id"))
{
memcpy(vendor_id, found, strlen(found));
is_vendor = true;
}
if (!model_found && checkAvailability(line, "model"))
{
memcpy(model, found, strlen(found));
model_found = true;
}
if (!is_model_name && checkAvailability(line, "model name"))
{
memcpy(model_name, found, strlen(found));
is_model_name = true;
}
if (checkAvailability(line, "cpu MHz"))
{
physical_processor++;
memcpy(cpu_mhz, found, strlen(found));
}
if (!is_cpu_cores && checkAvailability(line, "cpu cores"))
{
cpu_cores = atoi(found);
is_cpu_cores = true;
}
}
}
if (line != NULL)
{
free(line);
line = NULL;
}
line_size = getline(&line, &line_buf_size, file);
}
if (line != NULL)
{
free(line);
line = NULL;
}
fclose(file);
if (physical_processor)
{
cpu_hz = atof(cpu_mhz);
cpu_freq = (cpu_hz);
values[CPU_VENDOR] = CStringGetTextDatum(vendor_id);
values[MODEL_NAME] = CStringGetTextDatum(model_name);
values[PHY_PROCESSOR] = Int32GetDatum(physical_processor);
values[CORES] = Int32GetDatum(cpu_cores);
values[CLOCK_SPEED] = Int64GetDatumFast(cpu_freq);
tuplestore_putvalues(tupstore, tupdesc, values, nulls);
}
}
Datum ass2(PG_FUNCTION_ARGS)
{
ReturnSetInfo *rsinfo = (ReturnSetInfo *)fcinfo->resultinfo;
TupleDesc tupdesc;
Tuplestorestate *tupstore;
MemoryContext per_query_ctx;
MemoryContext oldcontext;
per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
oldcontext = MemoryContextSwitchTo(per_query_ctx);
if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
elog(ERROR, "return type must be a row type");
Assert(tupdesc->natts == SIZE);
tupstore = tuplestore_begin_heap(true, false, work_mem);
rsinfo->returnMode = SFRM_Materialize;
rsinfo->setResult = tupstore;
rsinfo->setDesc = tupdesc;
MemoryContextSwitchTo(oldcontext);
read_cpu_info(tupstore, tupdesc);
tuplestore_donestoring(tupstore);
return (Datum)0;
}