This repository has been archived by the owner on Apr 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
volumes.c
186 lines (155 loc) · 5.68 KB
/
volumes.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
/*
* Copyright (C) 2012 Hubert Kario <kario@wsisiz.edu.pl>
*
* 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 3 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include "config.h"
#include "volumes.h"
#include "activity_stats.h"
#include "lvmls.h"
// TODO stub
off_t
get_extent_size(struct program_params *pp, const char *lv_name)
{
return 4 * 1024 * 1024;
}
// TODO stub
const char *
get_first_volume_name(struct program_params *pp)
{
cfg_t *tmp;
tmp = cfg_getnsec(pp->cfg, "volume", 0);
return cfg_title(tmp);
}
// selects best or worst extents in collection not residing on specific devices
int extents_selector(struct extent_stats *es, struct extents **ret,
struct program_params *pp, const char *lv_name, int max_tier,
int max_extents, int hot_cold)
{
assert(ret);
assert(hot_cold == ES_HOT || hot_cold == ES_COLD);
// allocate memory for `max_extent` entires
*ret = malloc(sizeof(struct extents));
assert(*ret);
(*ret)->length = 0;
(*ret)->sort = hot_cold;
(*ret)->extents = malloc(sizeof(struct extent*) * max_extents);
assert((*ret)->extents); // TODO error handling
// a bit complicated for(;;), basically it searches from 0 when we're
// looking for hot extents and searches from last extent in `es` in case
// we're looking for cold extents. In both cases it doesn't load more than
// max_extents to ret
for(ssize_t i=(hot_cold == ES_HOT)?0:es->length-1;
(hot_cold == ES_HOT && i < es->length && (*ret)->length < max_extents)
|| (hot_cold == ES_COLD && i >= 0 && (*ret)->length < max_extents);
i = (hot_cold == ES_HOT)?i+1:i-1) {
int tier = get_extent_tier(pp, lv_name, &es->extents[i]);
if ((hot_cold == ES_HOT && max_tier < tier) ||
(hot_cold == ES_COLD && max_tier == tier)) {
(*ret)->extents[(*ret)->length] = &es->extents[i];
(*ret)->length++;
}
}
return 0;
}
// comparison function for sorting extents according to their score
static int
extent_compare(const void *v1, const void *v2)
{
struct extent *e1 = (struct extent *)v1;
struct extent *e2 = (struct extent *)v2;
if (e1->score > e2->score)
return -1;
if (e1->score < e2->score)
return 1;
return 0;
}
int
get_volume_stats(struct program_params *pp, const char *lv_name, struct extent_stats **es)
{
assert(es);
int f_ret = 0; // this function return code
int ret;
// use the same time base to calculate score of all extents
time_t now = time(NULL);
*es = malloc(sizeof(struct extent_stats));
if (!*es) {
fprintf(stderr, "Out of memory\n");
return -1;
}
struct activity_stats *as;
char *file;
asprintf(&file, "%s.lvmts", lv_name);
// read activity stats from file generated by lvmtsmd
ret = read_activity_stats(&as, file);
assert(!ret);
(*es)->extents = malloc(sizeof(struct extent) * as->len);
assert((*es)->extents); // XXX better error checking
(*es)->length = as->len;
// load LE to PE translation tables
init_le_to_pe(pp);
// collect general volume parameters
float read_mult = get_read_multiplier(pp, lv_name);
float write_mult = get_write_multiplier(pp, lv_name);
float hit_score = get_hit_score(pp, lv_name);
float scale = get_score_scaling_factor(pp, lv_name);
for(size_t i=0; i < as->len; i++) {
// just a shorthand, so that we wouldn't have to write full (*es)->...
struct extent *e = &((*es)->extents[i]);
// get logical extent to physical extent mapping for extent
struct pv_info *pv_i;
pv_i = LE_to_PE(get_volume_vg(pp, lv_name),
get_volume_lv(pp, lv_name),
i);
if (!pv_i) {
fprintf(stderr, "error when translating extent %li\n", i);
fprintf(stderr, "Do you have permission to access lvm device?\n");
abort();
}
// get activity stats for block
struct block_activity *ba = get_block_activity(as, i);
// save collected data
e->dev = strdup(pv_i->pv_name);
assert(e->dev); // TODO better error handling
e->le = i;
e->pe = pv_i->start_seg;
e->read_score =
get_block_activity_raw_score(ba, T_READ);
e->last_read_access = get_last_read_time(ba);
e->write_score =
get_block_activity_raw_score(ba, T_WRITE);
e->last_write_access = get_last_write_time(ba);
e->score = calculate_score( e->read_score,
e->last_read_access,
read_mult,
e->write_score,
e->last_write_access,
write_mult,
now,
scale);
pv_info_free(pv_i);
}
// clean up
destroy_activity_stats(as);
// sort according to score
qsort((*es)->extents, (*es)->length, sizeof(struct extent),
extent_compare);
return f_ret;
}