interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

branch master Branches Tags History

sqlite / tool / showtmlog.c

255 lines · 6364 bytes Raw Blame History
1/*
2** A utility program to decode tmstmpvfs log files.
3*/
4#include <stdio.h>
5#include <assert.h>
6#include <string.h>
7#include <stdint.h>
8#include <stdlib.h>
9
10/*
11** The six bytes at a[] are a big-endian unsigned integer which is the
12** number of milliseconds since 1970. Decode that value into an ISO 8601
13** date/time string stored in static space and return a pointer to that
14** string.
15*/
16static const char *decodeTimestamp(const unsigned char *a){
17 uint64_t ms; /* Milliseconds since 1970 */
18 uint64_t days; /* Days since 1970-01-01 */
19 uint64_t sod; /* Start of date specified by ms */
20 uint64_t z; /* Days since 0000-03-01 */
21 uint64_t era; /* 400-year era */
22 int i; /* Loop counter */
23 int h; /* hour */
24 int m; /* minute */
25 int s; /* second */
26 int f; /* millisecond */
27 int Y; /* year */
28 int M; /* month */
29 int D; /* day */
30 int y; /* year assuming March is first month */
31 unsigned int doe; /* day of 400-year era */
32 unsigned int yoe; /* year of 400-year era */
33 unsigned int doy; /* day of year */
34 unsigned int mp; /* month with March==0 */
35 static char zOut[50]; /* Return results here */
36
37 for(ms=0, i=0; i<=5; i++) ms = (ms<<8) + a[i];
38 if( ms==0 ){
39 return " ";
40 }else if( ms>4102444800000LL ){ /* 2100-01-01 */
41 /* YYYY-MM-DD HH:MM:SS.SSS */
42 return " (bad date) ";
43 }
44 days = ms/86400000;
45 sod = (ms%86400000)/1000;
46 f = (int)(ms%1000);
47
48 h = sod/3600;
49 m = (sod%3600)/60;
50 s = sod%60;
51 z = days + 719468;
52 era = z/146097;
53 doe = (unsigned)(z - era*146097);
54 yoe = (doe - doe/1460 + doe/36524 - doe/146096)/365;
55 y = (int)yoe + era*400;
56 doy = doe - (365*yoe + yoe/4 - yoe/100);
57 mp = (5*doy + 2)/153;
58 D = doy - (153*mp + 2)/5 + 1;
59 M = mp + (mp<10 ? 3 : -9);
60 Y = y + (M <=2);
61 snprintf(zOut, sizeof(zOut),
62 "%04d-%02d-%02d %02d:%02d:%02d.%03d",
63 Y, M, D, h, m, s, f);
64 return zOut;
65}
66
67/*
68** Render a single 16-byte tmstmpvfs log record as a line to a CSV file.
69**
70** Columns: tmstmp,fileno,op,pid,pgno,frame,salt,txn
71*/
72static void renderCSV(int iFile, unsigned char *a){
73 unsigned int a2, a3;
74 int j;
75 uint64_t ms;
76
77 for(ms=0, j=2; j<=7; j++) ms = (ms<<8) + a[j];
78 printf("%u.%03u,%d,", (unsigned int)(ms/1000), (unsigned)(ms%1000), iFile);
79 for(a2=0, j=8; j<=11; j++) a2 = (a2<<8)+a[j];
80 for(a3=0, j=12; j<=15; j++) a3 = (a3<<8)+a[j];
81 switch( a[0] ){
82 case 0x01: {
83 printf("\"open-db\",%u,,,,,0\r\n",a2);
84 break;
85 }
86 case 0x02: {
87 printf("\"open-wal\",%u,,,,,0\r\n", a2);
88 break;
89 }
90 case 0x03: {
91 printf("\"wal-page\",,%u,%u,,%d,%d\r\n", a2, a3, a[1]&1, (a[1]>>1)&1);
92 break;
93 }
94 case 0x04: {
95 printf("\"db-page\",,%u,,,,%d\r\n", a2,(a[1]>>1)&1);
96 break;
97 }
98 case 0x05: {
99 printf("\"ckpt-start\",,,,,,0\r\n");
100 break;
101 }
102 case 0x06: {
103 printf("\"ckpt-page\",,%u,%u,,%d\r\n", a2, a3, (a[1]>>1)&1);
104 break;
105 }
106 case 0x07: {
107 printf("\"ckpt-end\",,,,,,0\r\n");
108 break;
109 }
110 case 0x08: {
111 printf("\"wal-reset\",,,,%u,,0\r\n", a3);
112 break;
113 }
114 case 0x0e: {
115 printf("\"close-wal\",,,,,,0\r\n");
116 break;
117 }
118 case 0x0f: {
119 printf("\"close-db\",,,,,,0\r\n");
120 break;
121 }
122 default: {
123 printf("\"invalid-record\",,,,,,0\r\n");
124 break;
125 }
126 }
127}
128
129/*
130** Render a single 16-byte tmstmpvfs log record as human-readable text
131** on stdout.
132*/
133static void renderText(unsigned char *a){
134 unsigned int a2, a3;
135 int j;
136
137 printf("%s ", decodeTimestamp(a+2));
138 for(a2=0, j=8; j<=11; j++) a2 = (a2<<8)+a[j];
139 for(a3=0, j=12; j<=15; j++) a3 = (a3<<8)+a[j];
140 switch( a[0] ){
141 case 0x01: {
142 printf("open-db pid %u\n", a2);
143 break;
144 }
145 case 0x02: {
146 printf("open-wal pid %u\n", a2);
147 break;
148 }
149 case 0x03: {
150 static const char *za1[4] = { "", " txn", " zero", " txn zero" };
151 printf("wal-page pgno %-8u frame %-8u%s\n", a2, a3, za1[a[1]&3]);
152 break;
153 }
154 case 0x04: {
155 printf("db-page pgno %-8u%s\n", a2, (a[1]&2)!=0 ? " zero" : "");
156 break;
157 }
158 case 0x05: {
159 printf("ckpt-start\n");
160 break;
161 }
162 case 0x06: {
163 printf("ckpt-page pgno %-8u frame %-8u%s\n", a2, a3,
164 (a[1]&2)!=0 ? " zero" : "");
165 break;
166 }
167 case 0x07: {
168 printf("ckpt-end\n");
169 break;
170 }
171 case 0x08: {
172 printf("wal-reset salt1 0x%08x\n", a3);
173 break;
174 }
175 case 0x0e: {
176 printf("close-wal\n");
177 break;
178 }
179 case 0x0f: {
180 printf("close-db\n");
181 break;
182 }
183 default: {
184 printf("invalid-record\n");
185 break;
186 }
187 }
188}
189
190static void usage(const char *argv0){
191 printf("Usage: %s [--csv] LOGFILE ...\n", argv0);
192 printf("Decode one or more tmstmpvfs log files and display the results\n"
193 "on stdout. Render as CSV if the --csv option is used.\n");
194}
195
196int main(int argc, char **argv){
197 int i;
198 FILE *in;
199 unsigned char a[16];
200 int bCSV = 0;
201 const char *z;
202 int nFile = 0;
203 int iFile;
204 for(i=1; i<argc; i++){
205 z = argv[i];
206 if( z[0]=='-' ){
207 if( z[1]=='-' ) z++;
208 if( strcmp(z,"-csv")==0 ){
209 bCSV = 1;
210 }else
211 if( strcmp(z,"-help")==0 || strcmp(z,"-?")==0 ){
212 usage(argv[0]);
213 return 0;
214 }else
215 {
216 printf("unknown command-line option: \"%s\"\n",
217 argv[i]);
218 usage(argv[0]);
219 return 1;
220 }
221 }else{
222 nFile++;
223 }
224 }
225 if( nFile==0 ){
226 usage(argv[0]);
227 return 1;
228 }
229 iFile = 0;
230 if( bCSV ){
231 printf("tmstmp,fileno,op,pid,pgno,frame,salt,txn,zero\r\n");
232 }
233 for(i=1; i<argc; i++){
234 z = argv[i];
235 if( z[0]=='-' ) continue;
236 in = fopen(z, "rb");
237 if( in==0 ){
238 printf("%s: can't open\n", z);
239 continue;
240 }
241 iFile++;
242 if( nFile>1 && !bCSV ){
243 printf("*** %s ***\n", z);
244 }
245 while( 16==fread(a, 1, 16, in) ){
246 if( bCSV ){
247 renderCSV(iFile, a);
248 }else{
249 renderText(a);
250 }
251 }
252 fclose(in);
253 }
254 return 0;
255}