import sys
import os.path
import ctypes

# see include/pgstat.h
class PgStat_GlobalStats(ctypes.Structure):
    _pack_ = 4
    _fields_ = [
        ("format_id", ctypes.c_uint),
        ("stats_timestamp", ctypes.c_ulonglong),
        ("timed_checkpoints", ctypes.c_ulonglong),
        ("requested_checkpoints", ctypes.c_ulonglong),
        ("checkpoint_write_time", ctypes.c_ulonglong),
        ("checkpoint_sync_time", ctypes.c_ulonglong),
        ("buf_written_checkpoints", ctypes.c_ulonglong),
        ("buf_written_clean", ctypes.c_ulonglong),
        ("maxwritten_clean", ctypes.c_ulonglong),
        ("buf_written_backend", ctypes.c_ulonglong),
        ("buf_fsync_backend", ctypes.c_ulonglong),
        ("buf_alloc", ctypes.c_ulonglong),
        ("bgwriter_stat_reset_timestamp", ctypes.c_ulonglong),
        ("bytes_sent", ctypes.c_ulonglong),
        ("bytes_received", ctypes.c_ulonglong),
        ("socket_stat_reset_timestamp", ctypes.c_ulonglong),
    ]
    

if __name__ == '__main__':
    DATADIR = sys.argv[1]

    f = open(os.path.join(DATADIR, 'pg_stat/global.stat'), 'rb')
    s = PgStat_GlobalStats()
    f.readinto(s)
    
    print 'struct len: %d' % (ctypes.sizeof(s),)
    print 'format_id: %x' % (s.format_id)
    print 'bytes_sent: %d\nbytes_received: %d' % (s.bytes_sent, s.bytes_received)
    
#   for f, t in s._fields_:
#       print f, s.__getattribute__(f)
    