-
Notifications
You must be signed in to change notification settings - Fork 9
/
xsiostat_vbd.c
264 lines (225 loc) · 6.87 KB
/
xsiostat_vbd.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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
* -----------------------------
* XenServer Storage I/O Stats
* -----------------------------
* xsiostat_vbd.c
* ----------------
*
* Copyright (C) 2013, 2014 Citrix Systems Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation; version 2.1 only. with the special
* exception on linking described in file LICENSE.
*
* 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 Lesser General Public License for more details.
*/
// Header files
#define _GNU_SOURCE
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/queue.h>
#include <xenstore.h>
#include "xsiostat.h"
// Global variables
extern int PAGE_SIZE;
static uint32_t
vbd_read_tapdisk_pid(uint32_t domid, uint32_t vbdid)
{
// XenStore tapdisk pid path
static const char* PATH_FMT = "/local/domain/0/backend/vbd3/%u/%u/kthread-pid";
// Local variables
char *path; // Path storage
unsigned int len; // Temp variable
void *value; // Value returned by xs_read
uint32_t retvalue = 0; // Value converted to integer
// Open Xenstore connection
struct xs_handle *handle = xs_open(XS_OPEN_READONLY);
if (!handle) {
perror("xs_open");
goto err;
}
// Format tapdisk pid path
if (asprintf(&path, PATH_FMT, domid, vbdid) < 0) {
perror("asprintf");
goto asperr;
}
// Read value
value = xs_read(handle, XBT_NULL, path, &len);
if (!value) {
perror("xs_read");
goto readerr;
}
// Convert to int
retvalue = atoi((const char *)value);
free(value);
readerr:
free(path);
asperr:
xs_close(handle);
err:
return retvalue;
}
static void
vbd_free(xsis_vbd_t *vbd){
// Release VBD resources
if (vbd){
if (vbd->shmmap)
(void)munmap(vbd->shmmap, PAGE_SIZE);
if (vbd->shmfd >= 0)
(void)close(vbd->shmfd);
free(vbd);
}
}
static int
vbd_open(xsis_vbd_t **vbd, uint32_t domid, uint32_t vbdid){
// Local variables
char *ptr; // Temporary char pointer
int err = 0; // Return code
uint32_t tdpid = 0; // Tapdisk PID
// Allocate new VBD entry
if (!(*vbd = calloc(1, sizeof(xsis_vbd_t)))){
perror("calloc");
goto err;
}
tdpid = vbd_read_tapdisk_pid(domid, vbdid);
if (!tdpid) {
perror("vbd_read_tapdisk_pid");
goto err;
}
(*vbd)->domid = domid;
(*vbd)->vbdid = vbdid;
(*vbd)->tdpid = tdpid;
(*vbd)->shmfd = -1;
// Open stats fd
if (asprintf(&ptr, XSIS_TD3_PATHFMT, tdpid, domid, vbdid) < 0){
perror("asprintf");
goto err;
}
if (((*vbd)->shmfd = open(ptr, O_RDONLY)) < 0){
perror("open");
goto err;
}
// mmap() stats entry
if (!((*vbd)->shmmap = mmap(NULL, PAGE_SIZE, PROT_READ, MAP_SHARED,
(*vbd)->shmfd, 0))){
perror("mmap");
goto err;
}
out:
// Return
return(err);
err:
vbd_free(*vbd);
err = 1;
goto out;
}
int
vbd_update(xsis_vbd_t *vbd){
// Local variables
struct stat vbdst; // Temporary stat struct
int err = 0; // Return code
// Check if VBD remains valid
if (fstat(vbd->shmfd, &vbdst) || !(vbdst.st_nlink))
goto err;
// Update VBD entries based on shm mapping
vbd->tdstat.rop_1 = vbd->tdstat.rop_0;
vbd->tdstat.rop_0 = ((tapdisk_stats *)(vbd->shmmap))->read_reqs_submitted;
vbd->tdstat.rsc_1 = vbd->tdstat.rsc_0;
vbd->tdstat.rsc_0 = ((tapdisk_stats *)(vbd->shmmap))->read_sectors;
vbd->tdstat.wop_1 = vbd->tdstat.wop_0;
vbd->tdstat.wop_0 = ((tapdisk_stats *)(vbd->shmmap))->write_reqs_submitted;
vbd->tdstat.wsc_1 = vbd->tdstat.wsc_0;
vbd->tdstat.wsc_0 = ((tapdisk_stats *)(vbd->shmmap))->write_sectors;
vbd->tdstat.wtu_1 = vbd->tdstat.wtu_0;
vbd->tdstat.wtu_0 = ((tapdisk_stats *)(vbd->shmmap))->write_total_ticks;
vbd->tdstat.rtu_1 = vbd->tdstat.rtu_0;
vbd->tdstat.rtu_0 = ((tapdisk_stats *)(vbd->shmmap))->read_total_ticks;
vbd->tdstat.infrd = ((tapdisk_stats *)(vbd->shmmap))->read_reqs_submitted -
((tapdisk_stats *)(vbd->shmmap))->read_reqs_completed;
vbd->tdstat.infwr = ((tapdisk_stats *)(vbd->shmmap))->write_reqs_submitted -
((tapdisk_stats *)(vbd->shmmap))->write_reqs_completed;
vbd->tdstat.low_mem_mode = ((tapdisk_stats *)(vbd->shmmap))->flags
& BT3_LOW_MEMORY_MODE;
out:
// Return
return(err);
err:
err = 1;
goto out;
}
int
vbds_alloc(xsis_vbds_t *vbds, xsis_flts_t *domids, xsis_flts_t *vbdids){
// Local variables
DIR *dp = NULL; // dir pointer
struct dirent *dirp; // dirent pointer
xsis_vbd_t *vbd; // Temporary VBD pointer
uint32_t domid; // Temporary DOM ID
uint32_t vbdid; // Temporary VBD ID
int err = 0; // Return code
// Open VBD3 base dir
if (!(dp = opendir(XSIS_VBD3_DIR))){
perror("opendir");
goto err;
}
// Scan for valid VBD entries
while ((dirp = readdir(dp))){
// Skip irrelevant entries and fetch DOM/VBD ids
if (sscanf(dirp->d_name, XSIS_VBD3_BASEFMT, &domid, &vbdid) != 2)
continue;
// Filter domids and vbdids
if (!LIST_EMPTY(domids))
if (!flt_isset(domids, domid))
continue;
if (!LIST_EMPTY(vbdids))
if (!flt_isset(vbdids, vbdid))
continue;
// Do not add repeated entries
LIST_FOREACH(vbd, vbds, vbds)
if ((vbd->domid == domid) && (vbd->vbdid == vbdid))
break;
if (vbd)
continue;
// Allocate VBD entry
if (vbd_open(&vbd, domid, vbdid))
continue;
// Insert new VBD in list
LIST_INSERT_HEAD(vbds, vbd, vbds);
}
out:
// Close VBD3 base dir
if (dp)
closedir(dp);
// Return
return(err);
err:
err = 1;
goto out;
}
void
vbd_delete(xsis_vbd_t *vbd, xsis_vbds_t *vbds){
LIST_REMOVE(vbd, vbds);
vbd_free(vbd);
}
void
vbds_free(xsis_vbds_t *vbds){
// Local variables
xsis_vbd_t *vbd; // Temporary VBD pointer
// Loop through VBDs, freeing resources
LIST_FOREACH(vbd, vbds, vbds){
vbd_delete(vbd, vbds);
}
// Return
return;
}