summaryrefslogtreecommitdiff
path: root/src/pmdas/gfs2/latency.c
blob: 0b93ca96941f66d988bfe8f440703996d2946a0a (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
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
 * GFS2 latency metrics.
 *
 * Copyright (c) 2014 Red Hat.
 * 
 * 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.
 */

#include "pmapi.h"
#include "impl.h"
#include "pmda.h"
#include "pmdagfs2.h"

#include "ftrace.h"
#include "worst_glock.h"
#include "latency.h"

#include <string.h>
#include <inttypes.h>
#include <sys/sysmacros.h>
#include <sys/types.h>

static struct ftrace_data latency_data;
static int reset_flag;

static int latency_state = DEFAULT_LATENCY_STATE;

/*
 * Calculates the offset position for the flat array as if it was
 * a three dimentional array containing the latency values.
 */
int 
offset(int x, int y, int z)
{
    return (z * NUM_LATENCY_STATS * NUM_LATENCY_VALUES) + (y * NUM_LATENCY_STATS) + x ; 
}

/*
 * Fetches the value for the given metric item and then assigns to pmAtomValue.
 * We check to see if item is in valid range for the metric.
 */
int
gfs2_latency_fetch(int item, struct latency *latency, pmAtomValue *atom)
{
    int i, counter, position, results_used = 0;
    int64_t result = 0;

    /* We are assigning values so we want to reset on next extract */
    reset_flag = 1;

    /* Ensure that metric value wanted is valid */
    if ((item < 0 || item >= NUM_LATENCY_STATS))
	return PM_ERR_PMID;

    counter = latency->counter[item];

    /* Calculate latency for the metric (deduct start time and add the matching finish time) */
    for (i = 0; i < counter; i++) {
        position = offset(item, i, END);
        result += latency->values[position].usecs;

        position = offset(item, i, START);
        result -= latency->values[position].usecs;

        results_used++;
    }
    /* If we have no values return no values */
    if (results_used == 0)
        return 0;

    /* Return no values if negative result */
    if (result < 0 )
        return 0;

    /* Divide final value by number of counts */
    result /= results_used;

    /* Assign value to the metric */
    atom->ll = result;

    return 1;
}

/*
 * Sets the value of latency using pmstore, value
 * must be 0 or 1.
 */
int 
latency_set_state(pmValueSet *vsp)
{
    int value = vsp->vlist[0].value.lval;

    if (value == 0 || value == 1) {
        latency_state = value;

        return 0;
    } else {
        return PM_ERR_SIGN;
    }
}

/*
 * Used to see whether the latency metrics are enabled or disabled. Should
 * only return either 0 or 1.
 */
int
latency_get_state()
{
    return latency_state;
}

/*
 * Concatenates the ftrace time stamp (major and minor) components into one
 * uint64_t timestamp in usecs.
 *
 */
static int64_t 
concatenate(int64_t a, int64_t b)
{
    unsigned int power = 10;

    while(a >= power)
        power *= 10;

    return a * power + b;        
}

/*
 * Converts lock state to an integer
 */
static int
lock_to_decimal(char *state)
{
    if (strncmp(state, "NL", 2) == 0) {
        return 0;
    } else if (strncmp(state, "CR", 2) == 0) {
        return 1;
    } else if (strncmp(state, "CW", 2) == 0) {
        return 2;
    } else if (strncmp(state, "PR", 2) == 0) {
        return 3;
    } else if (strncmp(state, "PW", 2) == 0) {
        return 4;
    } else if (strncmp(state, "EX", 2) == 0) {
        return 5;
    }

    return 0;
}

/*
 * Updates the records held in the fs->latency structure with new latency
 * stats.
 */
static int
update_records(struct gfs2_fs *fs, int metric, struct latency_data data, int placement)
{
    int i, position, counter;
    struct latency_data blank = { 0 };

    counter = fs->latency.counter[metric];

    /* If we have an intial value */
    if (placement == START) {
        position = offset(metric, counter, START);
        fs->latency.values[position] = data;

        position = offset(metric, counter, END);
        fs->latency.values[position] = blank;

        fs->latency.counter[metric] = (counter + 1) % NUM_LATENCY_VALUES;

    /* If we have a final value */
    } else if (placement == END) {
        for (i = 0; i < counter; i++){
            position = offset(metric, i, START);

            if ((fs->latency.values[position].lock_type == data.lock_type) && 
                (fs->latency.values[position].number == data.number) &&
                (fs->latency.values[position].usecs < data.usecs )) {        

                position = offset(metric, i, END);
                fs->latency.values[position] = data;

                return 0;
            }
        }
    }
    return 0;
}

/*
 * We work out the individual metric values from our buffer input and store
 * them for processing after all of the values have been extracted from the
 * trace pipe.
 */
int
gfs2_extract_latency(unsigned int major, unsigned int minor, int tracepoint, char *data, pmInDom gfs_fs_indom)
{
    latency_data.dev_id = makedev(major, minor);
    latency_data.tracepoint = tracepoint;
    strncpy(latency_data.data, data, sizeof(latency_data.data)-1);

    int i, sts;
    struct gfs2_fs *fs;

    /* We walk through for each filesystem */
    for (pmdaCacheOp(gfs_fs_indom, PMDA_CACHE_WALK_REWIND);;) {
        if ((i = pmdaCacheOp(gfs_fs_indom, PMDA_CACHE_WALK_NEXT)) < 0)
	    break;
	sts = pmdaCacheLookup(gfs_fs_indom, i, NULL, (void **)&fs);
	if (sts != PMDA_CACHE_ACTIVE)
	    continue;

        /* Clear old entries if reset is set */
        if (reset_flag == 1) {
            memset(fs->latency.values, 0, sizeof fs->latency.values);
            memset(fs->latency.counter, 0, sizeof fs->latency.counter);
            reset_flag = 0;
        }

        /* Is the entry matching the filesystem we are on? */
        if (fs->dev_id != latency_data.dev_id)
            continue; 

        if (latency_data.tracepoint ==  GLOCK_QUEUE) {

            struct latency_data data;
            int64_t time_major, time_minor;
            char queue[8], state[3];

            sscanf(latency_data.data, 
                "%*s [%*d] %"SCNd64".%"SCNd64": gfs2_glock_queue: %*d,%*d glock %"SCNu32":%"SCNu64" %s %s",
                &time_major, &time_minor, &data.lock_type, &data.number, queue, state
            );
            data.usecs = concatenate(time_major, time_minor);

            if (data.lock_type == WORSTGLOCK_INODE || data.lock_type == WORSTGLOCK_RGRP) {

                /* queue trace data is used both for latency.grant and latency.queue */
                if (strncmp(queue, "queue", 6) == 0) {
                    if (strncmp(state, "NL", 2) == 0) {
                        update_records(fs, LATENCY_GRANT_NL, data, START);
                        update_records(fs, LATENCY_QUEUE_NL, data, START);
                    } else if (strncmp(state, "CR", 2) == 0) {
                        update_records(fs, LATENCY_GRANT_CR, data, START);
                        update_records(fs, LATENCY_QUEUE_CR, data, START);
                    } else if (strncmp(state, "CW", 2) == 0) {
                        update_records(fs, LATENCY_GRANT_CW, data, START);
                        update_records(fs, LATENCY_QUEUE_CW, data, START);
                    } else if (strncmp(state, "PR", 2) == 0) {
                        update_records(fs, LATENCY_GRANT_PR, data, START);
                        update_records(fs, LATENCY_QUEUE_PR, data, START);
                    } else if (strncmp(state, "PW", 2) == 0) {
                        update_records(fs, LATENCY_GRANT_PW, data, START);
                        update_records(fs, LATENCY_QUEUE_PW, data, START);
                    } else if (strncmp(state, "EX", 2) == 0) {
                        update_records(fs, LATENCY_GRANT_EX, data, START);
                        update_records(fs, LATENCY_QUEUE_EX, data, START);
                    }
                    update_records(fs, LATENCY_GRANT_ALL, data, START);
                    update_records(fs, LATENCY_QUEUE_ALL, data, START);

                } else if (strncmp(queue, "dequeue", 8) == 0) {
                    if (strncmp(state, "NL", 2) == 0) {
                        update_records(fs, LATENCY_QUEUE_NL, data, END);
                    } else if (strncmp(state, "CR", 2) == 0) {
                        update_records(fs, LATENCY_QUEUE_CR, data, END);
                    } else if (strncmp(state, "CW", 2) == 0) {
                        update_records(fs, LATENCY_QUEUE_CW, data, END);
                    } else if (strncmp(state, "PR", 2) == 0) {
                        update_records(fs, LATENCY_QUEUE_PR, data, END);
                    } else if (strncmp(state, "PW", 2) == 0) {
                        update_records(fs, LATENCY_QUEUE_PW, data, END);
                    } else if (strncmp(state, "EX", 2) == 0) {
                        update_records(fs, LATENCY_QUEUE_EX, data, END);
                    }
                    update_records(fs, LATENCY_QUEUE_ALL, data, END);
                }
            }
        } else if (latency_data.tracepoint ==  GLOCK_STATE_CHANGE) {

            struct latency_data data;
            int64_t time_major, time_minor;
            char state[3], to[3], target[3];
            int state_decimal, to_decimal;

            sscanf(latency_data.data, 
                "%*s [%*d] %"SCNd64".%"SCNd64": gfs2_glock_state_change: %*d,%*d glock %"SCNu32":%"SCNu64" state %s to %s tgt:%s dmt:%*s flags:%*s", 
                &time_major, &time_minor, &data.lock_type, &data.number, state, to, target
            );
            data.usecs = concatenate(time_major, time_minor);
            state_decimal = lock_to_decimal(state);
            to_decimal = lock_to_decimal(to);

            if (data.lock_type == WORSTGLOCK_INODE || data.lock_type == WORSTGLOCK_RGRP) {

                /* state change trace data is used both for latency.grant and latency.demote */
                if ((state_decimal < to_decimal) && (strncmp(to, target, 2) == 0)) {
                    if (strncmp(to, "NL", 2) == 0) {
                        update_records(fs, LATENCY_GRANT_NL, data, END);
                    } else if (strncmp(to, "CR", 2) == 0) {
                        update_records(fs, LATENCY_GRANT_CR, data, END);
                    } else if (strncmp(to, "CW", 2) == 0) {
                        update_records(fs, LATENCY_GRANT_CW, data, END);
                    } else if (strncmp(to, "PR", 2) == 0) {
                        update_records(fs, LATENCY_GRANT_PR, data, END);
                    } else if (strncmp(to, "PW", 2) == 0) {
                        update_records(fs, LATENCY_GRANT_PW, data, END);
                    } else if (strncmp(to, "EX", 2) == 0) {
                        update_records(fs, LATENCY_GRANT_EX, data, END);
                    }
                    update_records(fs, LATENCY_GRANT_ALL, data, END);

                } else if ((state_decimal > to_decimal) && (strncmp(to, target, 2) == 0)) {
                    if (strncmp(state, "NL", 2) == 0) {
                        update_records(fs, LATENCY_DEMOTE_NL, data, END);
                    } else if (strncmp(state, "CR", 2) == 0) {
                        update_records(fs, LATENCY_DEMOTE_CR, data, END);
                    } else if (strncmp(state, "CW", 2) == 0) {
                        update_records(fs, LATENCY_DEMOTE_CW, data, END);
                    } else if (strncmp(state, "PR", 2) == 0) {
                        update_records(fs, LATENCY_DEMOTE_PR, data, END);
                    } else if (strncmp(state, "PW", 2) == 0) {
                        update_records(fs, LATENCY_DEMOTE_PW, data, END);
                    } else if (strncmp(state, "EX", 2) == 0) {
                        update_records(fs, LATENCY_DEMOTE_EX, data, END);
                    }
                    update_records(fs, LATENCY_DEMOTE_ALL, data, END);
                }
            }
        } else if (latency_data.tracepoint ==  DEMOTE_RQ) {

            struct latency_data data;
            int64_t time_major, time_minor;
            char state[3];

            sscanf(latency_data.data, 
                "%*s [%*d] %"SCNd64".%"SCNd64": gfs2_demote_rq: %*d,%*d glock %"SCNu32":%"SCNu64" demote %s to %*s flags:%*s %*s", 
                &time_major, &time_minor, &data.lock_type, &data.number, state
            );
            data.usecs = concatenate(time_major, time_minor);

            if ((data.lock_type == WORSTGLOCK_INODE) || (data.lock_type == WORSTGLOCK_RGRP)) {

                /* demote rq trace data is used for latency.demote */
                if (strncmp(state, "NL", 2) == 0) {
                    update_records(fs, LATENCY_DEMOTE_NL, data, START);
                } else if (strncmp(state, "CR", 2) == 0) {
                    update_records(fs, LATENCY_DEMOTE_CR, data, START);
                } else if (strncmp(state, "CW", 2) == 0) {
                    update_records(fs, LATENCY_DEMOTE_CW, data, START);
                } else if (strncmp(state, "PR", 2) == 0) {
                    update_records(fs, LATENCY_DEMOTE_PR, data, START);
                } else if (strncmp(state, "PW", 2) == 0) {
                    update_records(fs, LATENCY_DEMOTE_PW, data, START);
                } else if (strncmp(state, "EX", 2) == 0) {
                    update_records(fs, LATENCY_DEMOTE_EX, data, START);
                }
                update_records(fs, LATENCY_DEMOTE_ALL, data, START);
            }
        }     
    }
    return 0;                       
}