Updates will be applied April 15th at 12pm EDT (UTC-0400). GitLab could be a little slow between 12 - 12:45pm EDT.

Commit 620a354b authored by Ben Boeckel's avatar Ben Boeckel

gen_memstats: add kpagecount to the database

Change-Id: Iaaa4cfee80d237ba30eb88916c3037a134f17001
parent 017220da
add_executable(gen_memstats
gen_memstats.cxx
read_kpagecount.cxx
read_map.cxx
read_pagemap.cxx)
target_link_libraries(gen_memstats
......
#include "read_kpagecount.h"
#include "read_map.h"
#include "read_pagemap.h"
#include <sqlite3.h>
#include <fstream>
#include <set>
#include <iostream>
#include <sys/types.h>
......@@ -50,14 +52,15 @@ typedef raii<sqlite3_stmt*> safe_sql_stmt;
int main(int argc, char* argv[]) {
if (argc < 4) {
std::cerr << "usage: " << argv[0] << " <maps> <pagemap> <rank> <database>" << std::endl;
std::cerr << "usage: " << argv[0] << " <maps> <pagemap> <kpagecount> <rank> <database>" << std::endl;
return EXIT_FAILURE;
}
const char* maps_path = argv[1];
const char* pagemap_path = argv[2];
int rank = atoi(argv[3]);
const char* database_path = argv[4];
const char* kpagecount_path = argv[3];
int rank = atoi(argv[4]);
const char* database_path = argv[5];
std::ifstream imaps(maps_path);
safe_fd ipagemap(open(pagemap_path, 0), close);
......@@ -65,6 +68,11 @@ int main(int argc, char* argv[]) {
perror("failed to open pagemap file");
return EXIT_FAILURE;
}
safe_fd ikpagecount(open(kpagecount_path, 0), close);
if (ikpagecount < 0) {
perror("failed to open kpagecount file");
return EXIT_FAILURE;
}
safe_sql sql(NULL, sqlite3_close);
if (sqlite3_open_v2(database_path, &sql.get(), SQLITE_OPEN_NOMUTEX | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL) != SQLITE_OK) {
......@@ -85,6 +93,7 @@ int main(int argc, char* argv[]) {
safe_sql_stmt sql_insert_map(NULL, sqlite3_finalize);
safe_sql_stmt sql_insert_pagemap(NULL, sqlite3_finalize);
safe_sql_stmt sql_insert_kpagecount(NULL, sqlite3_finalize);
if (sqlite3_prepare(sql,
"INSERT INTO maps VALUES ("
......@@ -123,6 +132,16 @@ int main(int argc, char* argv[]) {
sqlerror(sql, "preparing pagemap insert statement");
return EXIT_FAILURE;
}
if (sqlite3_prepare(sql,
"INSERT INTO kpagecount VALUES ("
" null,"
" :pfn,"
" :count"
");",
-1, &sql_insert_kpagecount.get(), NULL) != SQLITE_OK) {
sqlerror(sql, "preparing kpagecount insert statement");
return EXIT_FAILURE;
}
#define MAKE_INDEX(query, name) \
int query##_##name##_idx = sqlite3_bind_parameter_index(sql_insert_##query, ":" #name)
......@@ -150,8 +169,13 @@ int main(int argc, char* argv[]) {
MAKE_INDEX(pagemap, swap_offset);
MAKE_INDEX(pagemap, swap_type);
MAKE_INDEX(kpagecount, pfn);
MAKE_INDEX(kpagecount, count);
#undef MAKE_INDEX
std::set<uint64_t> pfns;
#define BIND(query, name, type, ...) \
if (sqlite3_bind_##type( \
sql_insert_##query, \
......@@ -164,6 +188,7 @@ int main(int argc, char* argv[]) {
const int pagesize = getpagesize();
map_t map;
pagemap_t pagemap;
kpagecount_t kpagecount;
BIND(map, rank, int, rank);
BIND(pagemap, rank, int, rank);
......@@ -208,6 +233,7 @@ int main(int argc, char* argv[]) {
if (pagemap.query.present) {
BIND(pagemap, pfn, int64, pagemap.present.pfn);
pfns.insert(pagemap.present.pfn);
} else {
BIND(pagemap, pfn, null);
}
......@@ -228,6 +254,22 @@ int main(int argc, char* argv[]) {
}
}
for (std::set<uint64_t>::const_iterator i = pfns.begin(); i != pfns.end(); ++i) {
int status = read_kpagecount(ikpagecount, *i, &kpagecount);
if (status < 0) {
return EXIT_FAILURE;
} else if (status) {
break;
}
BIND(kpagecount, pfn, int64, *i);
BIND(kpagecount, count, int, kpagecount);
if (run_query(sql_insert_kpagecount) != SQLITE_OK) {
sqlerror(sql, "inserting into kpagecount table");
}
}
if (sqlite3_exec(sql, "END TRANSACTION", NULL, NULL, NULL) != SQLITE_OK) {
sqlerror(sql, "ending transaction");
return EXIT_FAILURE;
......@@ -294,6 +336,20 @@ int create_tables(sqlite3* sql) {
return SQLITE_ERROR;
}
if (sqlite3_prepare(sql,
"CREATE TABLE IF NOT EXISTS kpagecount ("
" pfn INTEGER PRIMARY KEY NOT NULL,"
" count INTEGER NOT NULL"
");",
-1, &sql_create_tables.get(), NULL) != SQLITE_OK) {
sqlerror(sql, "preparing kpagecount table creation");
return SQLITE_ERROR;
}
if (sqlite3_step(sql_create_tables) != SQLITE_DONE) {
sqlerror(sql, "creating kpagecount table");
return SQLITE_ERROR;
}
return SQLITE_OK;
}
......
#include "read_kpagecount.h"
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
int read_kpagecount(int fd, uint64_t pfn, kpagecount_t* kpagecount) {
static const size_t KPAGECOUNT_ENTRY_SIZE = sizeof(uint64_t);
// Shifting by virt-addr-offset number of bytes and multiplying by the size
// of an address (the size of an entry in pagemap file)
off_t file_offset = pfn * KPAGECOUNT_ENTRY_SIZE;
if (lseek(fd, file_offset, SEEK_SET) < 0) {
if (errno) {
perror("lseek failed");
return -1;
}
return 1;
}
union {
uint64_t data;
char bytes[KPAGECOUNT_ENTRY_SIZE];
} buf;
size_t sz = 0;
while (sz < KPAGECOUNT_ENTRY_SIZE) {
errno = 0;
int csz = read(fd, (char*)buf.bytes + sz, KPAGECOUNT_ENTRY_SIZE - sz);
if (csz < 0) {
if (errno == EAGAIN) {
continue;
} else {
perror("failed to read");
return -1;
}
} else if (csz) {
sz += csz;
} else {
if (errno) {
perror("eof");
return -1;
}
return 1;
}
}
*kpagecount = buf.data;
return 0;
}
#include <stdint.h>
#include <unistd.h>
typedef uint64_t kpagecount_t;
int read_kpagecount(int fd, uint64_t pfn, kpagecount_t* kpagecount);
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment