diff options
author | Andreas Rumpf <rumpf_a@web.de> | 2016-11-21 12:07:17 +0100 |
---|---|---|
committer | Andreas Rumpf <rumpf_a@web.de> | 2016-11-21 12:10:44 +0100 |
commit | 02a2180a6a7f819add2e53053ccabfd80a59718d (patch) | |
tree | 135fcae1b038bd17e07ccb2a752ebda8598e999a /lib/pure/nimtracker.nim | |
parent | fa101a722f80646bcbf86b94b4c2ce2a4dce93a8 (diff) | |
download | Nim-02a2180a6a7f819add2e53053ccabfd80a59718d.tar.gz |
first version of the new memory tracking feature
Diffstat (limited to 'lib/pure/nimtracker.nim')
-rw-r--r-- | lib/pure/nimtracker.nim | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/lib/pure/nimtracker.nim b/lib/pure/nimtracker.nim new file mode 100644 index 000000000..e3d9832c6 --- /dev/null +++ b/lib/pure/nimtracker.nim @@ -0,0 +1,71 @@ +# +# +# Nim's Runtime Library +# (c) Copyright 2016 Andreas Rumpf +# +# See the file "copying.txt", included in this +# distribution, for details about the copyright. +# + +## Memory tracking support for Nim. + +when isMainModule: + import db_sqlite + var db = open("memtrack.db", "", "", "") + db.exec sql""" + create table if not exists Tracking( + id integer primary key, + op varchar not null, + address integer not null, + size integer not null, + file varchar not null, + line integer not null + )""" + db.close() +else: + when not defined(memTracker): + {.error: "Memory tracking support is turned off!".} + + {.push memtracker: off.} + # we import the low level wrapper and are careful not to use Nim's + # memory manager for anything here. + import sqlite3 + + var + dbHandle: PSqlite3 + insertStmt: Pstmt + + template sbind(x: int; value) = + when value is cstring: + let ret = insertStmt.bindText(x, value, value.len.int32, SQLITE_TRANSIENT) + if ret != SQLITE_OK: + quit "could not bind value" + else: + let ret = insertStmt.bindInt64(x, value) + if ret != SQLITE_OK: + quit "could not bind value" + + proc logEntries(log: TrackLog) {.nimcall.} = + for i in 0..log.count-1: + var success = false + let e = log.data[i] + discard sqlite3.reset(insertStmt) + discard clearBindings(insertStmt) + sbind 1, e.op + sbind(2, cast[int](e.address)) + sbind 3, e.size + sbind 4, e.file + sbind 5, e.line + if step(insertStmt) == SQLITE_DONE: + success = true + if not success: + quit "could not write to database!" + + if sqlite3.open("memtrack.db", dbHandle) == SQLITE_OK: + const query = "INSERT INTO tracking(op, address, size, file, line) values (?, ?, ?, ?, ?)" + if prepare_v2(dbHandle, query, + query.len, insertStmt, nil) == SQLITE_OK: + setTrackLogger logEntries + else: + quit "could not prepare statement" + {.pop.} |