File: circular_buffer.c

package info (click to toggle)
lua-sandbox-extensions 0~git20161128-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,596 kB
  • ctags: 1,458
  • sloc: ansic: 4,402; cpp: 2,102; makefile: 8
file content (1010 lines) | stat: -rw-r--r-- 26,522 bytes parent folder | download
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/** @brief Lua circular buffer implementation @file */

#include <ctype.h>
#include <float.h>
#include <math.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include "lauxlib.h"
#include "lua.h"
#include "lualib.h"

#ifdef _WIN32
#define snprintf _snprintf
#endif

#ifdef _MSC_VER
#pragma warning( disable : 4056 )
#pragma warning( disable : 4756 )
#endif

#ifdef LUA_SANDBOX
#include "luasandbox_output.h"
#include "luasandbox_serialize.h"
#endif

#define COLUMN_NAME_SIZE 16
#define UNIT_LABEL_SIZE 8

static const char *mozsvc_circular_buffer = "mozsvc.circular_buffer";
static const char *mozsvc_circular_buffer_table = "circular_buffer";

static const char *agg_methods[] = { "sum", "min", "max", "none", NULL };
static const char *default_unit = "count";

#if defined(_MSC_VER)
static const char *not_a_number = "nan";
#endif

typedef enum {
  AGGREGATION_SUM,
  AGGREGATION_MIN,
  AGGREGATION_MAX,
  AGGREGATION_NONE,
} COLUMN_AGGREGATION;

typedef enum {
  OUTPUT_CBUF,
  OUTPUT_CBUFD,
} OUTPUT_FORMAT;

typedef struct
{
  char name[COLUMN_NAME_SIZE];
  char unit[UNIT_LABEL_SIZE];
  COLUMN_AGGREGATION aggregation;
} header_info;

typedef struct circular_buffer
{
  time_t        current_time;
  unsigned      seconds_per_row;
  unsigned      current_row;
  unsigned      rows;
  unsigned      columns;
  unsigned      tcolumns; // columns * 2 since we now store the deltas in-line
  OUTPUT_FORMAT format;
  int           ref;

  header_info   *headers;
  double        values[];
} circular_buffer;


static time_t get_start_time(circular_buffer *cb)
{
  return cb->current_time - (cb->seconds_per_row * (cb->rows - 1));
}


static void copy_cleared_row(circular_buffer *cb, double *cleared, size_t rows)
{
  size_t pool = 1;
  size_t ask;

  while (rows > 0) {
    if (rows >= pool) {
      ask = pool;
    } else {
      ask = rows;
    }
    memcpy(cleared + (pool * cb->tcolumns), cleared,
           sizeof(double) * cb->tcolumns * ask);
    rows -= ask;
    pool += ask;
  }
}


static void clear_rows(circular_buffer *cb, unsigned num_rows)
{
  if (num_rows >= cb->rows) {
    num_rows = cb->rows;
  }
  unsigned row = cb->current_row;
  ++row;
  if (row >= cb->rows) {row = 0;}
  for (unsigned c = 0; c < cb->tcolumns; ++c) {
    cb->values[(row * cb->tcolumns) + c] = NAN;
  }
  double *cleared = &cb->values[row * cb->tcolumns];
  if (row + num_rows - 1 >= cb->rows) {
    copy_cleared_row(cb, cleared, cb->rows - row - 1);
    for (unsigned c = 0; c < cb->tcolumns; ++c) {
      cb->values[c] = NAN;
    }
    copy_cleared_row(cb, cb->values, row + num_rows - 1 - cb->rows);
  } else {
    copy_cleared_row(cb, cleared, num_rows - 1);
  }
}


static int cb_new(lua_State *lua)
{
  int n = lua_gettop(lua);
  luaL_argcheck(lua, n >= 3 && n <= 3, 0, "incorrect number of arguments");
  int rows = luaL_checkint(lua, 1);
  luaL_argcheck(lua, 1 < rows, 1, "rows must be > 1");
  int columns = luaL_checkint(lua, 2);
  luaL_argcheck(lua, 0 < columns &&  256 >= columns, 2,
                "columns must be > 0 and <= 256");
  int seconds_per_row = luaL_checkint(lua, 3);
  luaL_argcheck(lua, 0 < seconds_per_row, 3, "seconds_per_row is out of range");

  size_t header_bytes = sizeof(header_info) * columns;
  size_t buffer_bytes = sizeof(double) * rows * columns * 2;
  size_t struct_bytes = sizeof(circular_buffer);

  size_t nbytes = header_bytes + buffer_bytes + struct_bytes;
  circular_buffer *cb = (circular_buffer *)lua_newuserdata(lua, nbytes);
  cb->ref = LUA_NOREF;
  cb->format = OUTPUT_CBUF;
  cb->headers = (header_info *)&cb->values[rows * columns * 2];

  luaL_getmetatable(lua, mozsvc_circular_buffer);
  lua_setmetatable(lua, -2);

  cb->current_time = seconds_per_row * (rows - 1);
  cb->current_row = rows - 1;
  cb->rows = rows;
  cb->columns = columns;
  cb->tcolumns = columns * 2;
  cb->seconds_per_row = seconds_per_row;
  memset(cb->headers, 0, header_bytes);
  for (unsigned col = 0; col < cb->columns; ++col) {
    snprintf(cb->headers[col].name, COLUMN_NAME_SIZE,
             "Column_%d", col + 1);
    strncpy(cb->headers[col].unit, default_unit,
            UNIT_LABEL_SIZE - 1);
  }
  clear_rows(cb, rows);
  return 1;
}


static circular_buffer* check_circular_buffer(lua_State *lua, int min_args)
{
  circular_buffer *cb = luaL_checkudata(lua, 1, mozsvc_circular_buffer);
  luaL_argcheck(lua, min_args <= lua_gettop(lua), 0,
                "incorrect number of arguments");
  return cb;
}


static int check_row(circular_buffer *cb, double ns, int advance)
{
  time_t t = (time_t)(ns / 1e9);
  t = t - (t % cb->seconds_per_row);

  int current_row = (int)(cb->current_time / cb->seconds_per_row);
  int requested_row = (int)(t / cb->seconds_per_row);
  int row_delta = requested_row - current_row;
  int row = requested_row % cb->rows;

  if (row_delta > 0 && advance) {
    clear_rows(cb, row_delta);
    cb->current_time = t;
    cb->current_row = row;
  } else if (requested_row > current_row
             || abs(row_delta) >= (int)cb->rows) {
    return -1;
  }
  return row;
}


static int check_column(lua_State *lua, circular_buffer *cb, int arg)
{
  unsigned column = luaL_checkint(lua, arg);
  luaL_argcheck(lua, 1 <= column && column <= cb->columns, arg,
                "column out of range");
  --column; // make zero based
  return column;
}


static int cb_add(lua_State *lua)
{
  circular_buffer *cb = check_circular_buffer(lua, 4);
  double ns = luaL_checknumber(lua, 2);
  int row = check_row(cb, ns, 1); // advance the buffer forward if necessary
  int column = check_column(lua, cb, 3);
  double value = luaL_checknumber(lua, 4);

  if (row != -1) {
    int i = (row * cb->tcolumns) + column * 2;
    double old = cb->values[i];

    if (isnan(old)) {
      cb->values[i] = value;
    } else {
      if (isnan(value)) {
        luaL_error(lua, "cannot uninitialize a value");
      }
      cb->values[i] += value;
      if (isnan(cb->values[i])) {
        luaL_error(lua, "add produced a NAN");
      }
    }
    lua_pushnumber(lua, cb->values[i]);
    if (old == cb->values[i]) return 1;

    switch (cb->headers[column].aggregation) {
    case AGGREGATION_SUM:
      if (isnan(cb->values[i + 1])) {
        cb->values[i + 1] = value;
      } else {
        cb->values[i + 1] += value;
      }
      break;
    case AGGREGATION_MIN:
    case AGGREGATION_MAX:
      cb->values[i + 1] = cb->values[i];
      break;
    default:
      // none
      break;
    }
  } else {
    lua_pushnil(lua);
  }
  return 1;
}


static int cb_get(lua_State *lua)
{
  circular_buffer *cb = check_circular_buffer(lua, 3);
  int row = check_row(cb, luaL_checknumber(lua, 2), 0);
  int column = check_column(lua, cb, 3);
  lua_Integer offset = lua_tointeger(lua, lua_upvalueindex(1));

  if (row != -1) {
    lua_pushnumber(lua, cb->values[(row * cb->tcolumns) + column * 2 + offset]);
  } else {
    lua_pushnil(lua);
  }
  return 1;
}


static int cb_get_configuration(lua_State *lua)
{
  circular_buffer *cb = check_circular_buffer(lua, 1);

  lua_pushnumber(lua, cb->rows);
  lua_pushnumber(lua, cb->columns);
  lua_pushnumber(lua, cb->seconds_per_row);
  return 3;
}


static int cb_set(lua_State *lua)
{
  circular_buffer *cb = check_circular_buffer(lua, 4);
  double ns = luaL_checknumber(lua, 2);
  int row = check_row(cb, ns, 1); // advance the buffer forward if necessary
  int column = check_column(lua, cb, 3);
  double value = luaL_checknumber(lua, 4);

  if (row != -1) {
    int i = (row * cb->tcolumns) + column * 2;
    double old = cb->values[i];
    if (isnan(value) && !isnan(old)) {
      luaL_error(lua, "cannot uninitialize a value");
    }
    switch (cb->headers[column].aggregation) {
    case AGGREGATION_SUM:
      cb->values[i] = value;
      if (isfinite(old)) {
        value -= old;
        if (value == 0) break;
      }
      if (isnan(cb->values[i + 1])) {
        cb->values[i + 1] = value;
      } else {
        cb->values[i + 1] += value;
      }
      break;
    case AGGREGATION_MIN:
      if (isnan(old) || value < old) {
        cb->values[i] = value;
        cb->values[i + 1] = value;
      }
      break;
    case AGGREGATION_MAX:
      if (isnan(old) || value > old) {
        cb->values[i] = value;
        cb->values[i + 1] = value;
      }
      break;
    default:
      cb->values[i] = value;
      break;
    }
    lua_pushnumber(lua, cb->values[i]);
  } else {
    lua_pushnil(lua);
  }
  return 1;
}


static int cb_set_header(lua_State *lua)
{
  circular_buffer *cb = check_circular_buffer(lua, 3);
  int column = check_column(lua, cb, 2);
  const char *name = luaL_checkstring(lua, 3);
  const char *unit = luaL_optstring(lua, 4, default_unit);
  cb->headers[column].aggregation = luaL_checkoption(lua, 5, "sum",
                                                     agg_methods);

  strncpy(cb->headers[column].name, name, COLUMN_NAME_SIZE - 1);
  char *n = cb->headers[column].name;
  for (int j = 0; n[j] != 0; ++j) {
    if (!isalnum(n[j])) {
      n[j] = '_';
    }
  }
  strncpy(cb->headers[column].unit, unit, UNIT_LABEL_SIZE - 1);
  n = cb->headers[column].unit;
  for (int j = 0; n[j] != 0; ++j) {
    if (n[j] != '/' && n[j] != '*' && !isalnum(n[j])) {
      n[j] = '_';
    }
  }

  lua_pushinteger(lua, column + 1); // return the 1 based Lua column
  return 1;
}


static int cb_get_header(lua_State *lua)
{
  circular_buffer *cb = check_circular_buffer(lua, 2);
  int column = check_column(lua, cb, 2);

  lua_pushstring(lua, cb->headers[column].name);
  lua_pushstring(lua, cb->headers[column].unit);
  lua_pushstring(lua, agg_methods[cb->headers[column].aggregation]);
  return 3;
}


static int cb_get_range(lua_State *lua)
{
  circular_buffer *cb = check_circular_buffer(lua, 2);
  int column = check_column(lua, cb, 2);
  lua_Integer offset = lua_tointeger(lua, lua_upvalueindex(1));

  // optional range arguments
  double start_ns = luaL_optnumber(lua, 3, get_start_time(cb) * 1e9);
  double end_ns = luaL_optnumber(lua, 4, cb->current_time * 1e9);
  luaL_argcheck(lua, end_ns >= start_ns, 4, "end must be >= start");

  int start_row = check_row(cb, start_ns, 0);
  int end_row = check_row(cb, end_ns, 0);
  if (-1 == start_row || -1 == end_row) {
    lua_pushnil(lua);
    return 1;
  }

  lua_newtable(lua);
  int row = start_row;
  int i = 0;
  do {
    if (row == (int)cb->rows) {
      row = 0;
    }
    lua_pushnumber(lua, cb->values[(row * cb->tcolumns) + column * 2 + offset]);
    lua_rawseti(lua, -2, ++i);
  } while (row++ != end_row);

  return 1;
}


static int cb_current_time(lua_State *lua)
{
  circular_buffer *cb = check_circular_buffer(lua, 0);
  lua_pushnumber(lua, cb->current_time * 1e9);
  return 1; // return the current time
}


static int cb_version(lua_State *lua)
{
  lua_pushstring(lua, DIST_VERSION);
  return 1;
}


#ifdef LUA_SANDBOX
static void escape_annotation(lua_State *lua, const char *anno)
{
  luaL_Buffer b;
  luaL_buffinit(lua, &b);
  for (int i = 0; anno[i]; ++i) {
    switch (anno[i]) {
    case '\\':
    case '"':
    case '/':
      luaL_addchar(&b, '\\');
      luaL_addchar(&b, anno[i]);
      break;
    case '\b':
      luaL_addstring(&b, "\\b");
      break;
    case '\t':
      luaL_addstring(&b, "\\t");
      break;
    case '\n':
      luaL_addstring(&b, "\\n");
      break;
    case '\f':
      luaL_addstring(&b, "\\f");
      break;
    case '\r':
      luaL_addstring(&b, "\\r");
      break;
    default:
      if (isprint(anno[i])) {
        luaL_addchar(&b, anno[i]);
      } else {
        luaL_addchar(&b, ' ');
      }
    }
  }
  luaL_pushresult(&b);
}


static int cb_annotate(lua_State *lua)
{
  static const char *atypes[] = { "info", "alert", NULL };

  circular_buffer *cb = check_circular_buffer(lua, 5);
  double ns = luaL_checknumber(lua, 2);
  int row = check_row(cb, ns, 0);
  int column = check_column(lua, cb, 3);
  int atidx = luaL_checkoption(lua, 4, NULL, atypes);
  const char *annotation = luaL_checkstring(lua, 5);
  int delta = 1;
  switch (lua_type(lua, 6)) {
  case LUA_TNONE:
  case LUA_TNIL:
    break;
  case LUA_TBOOLEAN:
    delta = lua_toboolean(lua, 6);
    break;
  default:
    luaL_argerror(lua, 6, "delta must be boolean");
    break;
  }
  if (row == -1) {return 0;}

  time_t t = (time_t)(ns / 1e9);
  t = t - (t % cb->seconds_per_row);
  lua_getglobal(lua, mozsvc_circular_buffer_table);
  if (lua_istable(lua, -1)) {
    if (cb->ref == LUA_NOREF) {
      lua_newtable(lua);
      cb->ref = luaL_ref(lua, -2);
    }
    // get the annotation ref table for this cbuf
    lua_rawgeti(lua, -1, cb->ref);
    if (!lua_istable(lua, -1)) {
      return luaL_error(lua, "Could not find the annotation ref table");
    }

    // get the annotation row table using the timestamp
    lua_rawgeti(lua, -1, (int)t);
    if (!lua_istable(lua, -1)) {
      lua_pop(lua, 1); // remove non table entry
      lua_newtable(lua);
      lua_pushvalue(lua, -1);
      lua_rawseti(lua, -3, (int)t);
    }

    // get annotation column table
    lua_rawgeti(lua, -1, column + 1);
    if (!lua_istable(lua, -1)) {
      lua_pop(lua, 1); // remove non table entry
      lua_newtable(lua);
      lua_pushvalue(lua, -1);
      lua_rawseti(lua, -3, column + 1);
    }

    // create/overwrite table values
    lua_pushstring(lua, atypes[atidx]);
    lua_setfield(lua, -2, "type");

    escape_annotation(lua, annotation);
    lua_setfield(lua, -2, "annotation");

    if (delta) {
      lua_pushboolean(lua, delta);
      lua_setfield(lua, -2, "delta");
    }

    lua_pop(lua, 3); // remove ref table, row table, column table
  } else {
    luaL_error(lua, "Could not find table %s", mozsvc_circular_buffer_table);
  }
  lua_pop(lua, 1); // remove the circular buffer table or failed nil
  return 0;
}


static int cb_format(lua_State *lua)
{
  static const char *output_types[] = { "cbuf", "cbufd", NULL };
  circular_buffer *cb = check_circular_buffer(lua, 2);
  luaL_argcheck(lua, 2 == lua_gettop(lua), 0,
                "incorrect number of arguments");

  cb->format = luaL_checkoption(lua, 2, NULL, output_types);
  lua_pop(lua, 1); // remove the format
  return 1; // return the circular buffer object
}


static void read_time_row(char **p, circular_buffer *cb)
{
  cb->current_time = (time_t)strtoll(*p, &*p, 10);
  cb->current_row = strtoul(*p, &*p, 10);
}


static int read_double(char **p, double *value)
{
  while (**p && isspace(**p)) {
    ++*p;
  }
  if (!**p) return 0;

  char *end = NULL;
#ifdef _MSC_VER
  if ((*p)[0] == 'n' && strncmp(*p, not_a_number, 3) == 0) {
    *p += 3;
    *value = NAN;
  } else if ((*p)[0] == 'i' && strncmp(*p, "inf", 3) == 0) {
    *p += 3;
    *value = INFINITY;
  } else if ((*p)[0] == '-' && strncmp(*p, "-inf", 4) == 0) {
    *p += 4;
    *value = -INFINITY;
  } else {
    *value = strtod(*p, &end);
  }
#else
  *value = strtod(*p, &end);
#endif
  if (*p == end) {
    return 0;
  }
  *p = end;
  return 1;
}


static void cbufd_fromstring(lua_State *lua,
                             circular_buffer *cb,
                             char **p)
{
  double value, ns = 0;
  size_t pos = 0;
  int row = -1;
  while (read_double(&*p, &value)) {
    if (pos == 0) { // new row, starts with a time_t
      ns = value * 1e9;
      row = check_row(cb, ns, 0);
    } else {
      if (row != -1) {
        cb->values[(row * cb->tcolumns) + (pos - 1) * 2 + 1] = value;
      }
    }
    if (pos == cb->columns) {
      pos = 0;
    } else {
      ++pos;
    }
  }
  if (pos != 0) {
    lua_pushstring(lua, "fromstring() invalid delta");
    lua_error(lua);
  }
  return;
}


static int cb_fromstring(lua_State *lua)
{
  circular_buffer *cb = check_circular_buffer(lua, 2);
  const char *values = luaL_checkstring(lua, 2);

  char *p = (char *)values;
  read_time_row(&p, cb);

  size_t pos = 0;
  size_t len = cb->rows * cb->columns;
  double value;
  while (pos < len && read_double(&p, &value)) {
    cb->values[pos++ * 2] = value;
  }

  if (pos == len) {
    cbufd_fromstring(lua, cb, &p);
  } else {
    luaL_error(lua, "fromstring() too few values: %d, expected %d", pos, len);
  }
  if (read_double(&p, &value)) {
    luaL_error(lua, "fromstring() too many values, more than: %d", len);
  }
  return 0;
}

static int output_cbuf(circular_buffer *cb, lsb_output_buffer *ob)
{
  unsigned col;
  unsigned row = cb->current_row + 1;
  for (unsigned i = 0; i < cb->rows; ++i, ++row) {
    if (row >= cb->rows) row = 0;
    for (col = 0; col < cb->columns; ++col) {
      if (col != 0) {
        if (lsb_outputc(ob, '\t')) return 1;
      }
      if (lsb_outputd(ob,
                      cb->values[(row * cb->tcolumns) + col * 2])) {
        return 1;
      }
    }
    if (lsb_outputc(ob, '\n')) return 1;
  }
  return 0;
}


static bool is_row_dirty(circular_buffer *cb, unsigned row)
{
  bool dirty = false;
  for (unsigned col = 0; col < cb->columns; ++col) {
    if (!isnan(cb->values[(row * cb->tcolumns) + col * 2 + 1])) {
      dirty = true;
      break;
    }
  }
  return dirty;
}


static int
output_cbufd(circular_buffer *cb, lsb_output_buffer *ob, bool serialize)
{
  char sep = '\t';
  char eol = '\n';
  if (serialize) {
    sep = ' ';
    eol = ' ';
  }
  long long t = get_start_time(cb);
  unsigned col;
  unsigned row = cb->current_row + 1;
  for (unsigned i = 0; i < cb->rows; ++i, ++row) {
    if (row >= cb->rows) {
      row = 0;
    }
    if (is_row_dirty(cb, row)) {
      if (lsb_outputf(ob, "%lld", t)) return 1;
      for (col = 0; col < cb->columns; ++col) {
        if (lsb_outputc(ob, sep)) return 1;
        int idx = (row * cb->tcolumns) + col * 2 + 1;
        if (lsb_outputd(ob, cb->values[idx])) return 1;
        cb->values[idx] = NAN;
      }
      if (lsb_outputc(ob, eol)) return 1;
    }
    t += cb->seconds_per_row;
  }
  return 0;
}


static int
output_annotations(lua_State *lua, circular_buffer *cb, lsb_output_buffer *ob,
                   const char *key)
{
  if (cb->ref == LUA_NOREF) return 0;

  lua_getglobal(lua, mozsvc_circular_buffer_table);
  if (lua_istable(lua, -1)) {
    lua_rawgeti(lua, -1, cb->ref); // get the annotation table for this cbuf
    if (!lua_istable(lua, -1)) {
      lua_pop(lua, 2); // remove value and cbuf table
      return 0;
    }
    lua_pushnil(lua);
    bool first = true;
    time_t st = get_start_time(cb);
    while (lua_next(lua, -2) != 0) {
      if (!lua_istable(lua, -1)) {
        luaL_error(lua, "Invalid annotation table value");
      }
      if (!lua_isnumber(lua, -2)) {
        luaL_error(lua, "Invalid annotation table key");
      }

      time_t ti = (time_t)lua_tointeger(lua, -2);
      if (ti < st) {
        lua_pop(lua, 1);        // remove the table value
        lua_pushvalue(lua, -1); // duplicate the key
        lua_pushnil(lua);
        lua_rawset(lua, -4);    // prune the old entry
        continue;
      }

      for (unsigned col = 1; col <= cb->columns; ++col) {
        lua_rawgeti(lua, -1, col);
        if (lua_type(lua, -1) == LUA_TTABLE) {
          lua_getfield(lua, -1, "delta");
          bool delta = lua_toboolean(lua, -1);
          lua_pop(lua, 1);

          bool output = true;
          if (!key && OUTPUT_CBUFD == cb->format) {
            if (delta) {
              lua_pushnil(lua);
              lua_setfield(lua, -2, "delta");
            } else {
              output = false;
            }
          }
          if (output) {
            lua_getfield(lua, -1, "annotation");
            const char *annotation = lua_tostring(lua, -1);
            size_t len;
            lua_getfield(lua, -2, "type");
            const char *atype = lua_tolstring(lua, -1, &len);
            if (!annotation || !atype || len == 0) {
              luaL_error(lua, "malformend annotation table");
            }
            if (key) {
              if (lsb_outputf(ob, "%s:annotate(%g, %u, \"%s\", \"%s\", %s)\n",
                              key,
                              ti * 1e9,
                              col,
                              atype,
                              annotation,
                              delta ? "true" : "false")) {
                return 1;
              }
            } else {
              if (first) {
                first = false;
              } else {
                if (lsb_outputc(ob, ',')) return 1;
              }
              if (lsb_outputf(ob, "{\"x\":%lld,"
                              "\"col\":%u,"
                              "\"shortText\":\"%c\","
                              "\"text\":\"%s\"}",
                              ti * 1000LL, col, atype[0], annotation)) {
                return 1;
              }
            }
            lua_pop(lua, 2); // remove atype and text
          }
        }
        lua_pop(lua, 1); // remove the column table
      }
      lua_pop(lua, 1); // remove the value, keep the key
    }
    lua_pop(lua, 1); // remove the annotation table
  } else {
    luaL_error(lua, "Could not find table %s", mozsvc_circular_buffer_table);
  }
  lua_pop(lua, 1); // remove the circular buffer table or failed nil
  return 0;
}


static int cb_output(lua_State *lua)
{
  lsb_output_buffer *ob = lua_touserdata(lua, -1);
  circular_buffer *cb = lua_touserdata(lua, -2);
  if (!(ob && cb)) {
    return 1;
  }

  size_t pos;
  bool has_anno = false;
  if (lsb_outputf(ob,
                  "{\"time\":%lld,\"rows\":%d,\"columns\":%d,\""
                  "seconds_per_row\":%d,\"column_info\":[",
                  (long long)get_start_time(cb),
                  cb->rows,
                  cb->columns,
                  cb->seconds_per_row)) {
    return 1;
  }

  for (unsigned col = 0; col < cb->columns; ++col) {
    if (col != 0) {
      if (lsb_outputc(ob, ',')) return 1;
    }
    if (lsb_outputf(ob, "{\"name\":\"%s\",\"unit\":\"%s\",\""
                    "aggregation\":\"%s\"}",
                    cb->headers[col].name,
                    cb->headers[col].unit,
                    agg_methods[cb->headers[col].aggregation])) {
      return 1;
    }
  }
  if (lsb_outputs(ob, "],\"annotations\":[", 17)) return 1;
  pos = ob->pos;
  if (output_annotations(lua, cb, ob, NULL)) return 1;
  if (pos != ob->pos) has_anno = true;
  if (lsb_outputs(ob, "]}\n", 3)) return 1;

  if (OUTPUT_CBUFD == cb->format) {
    pos = ob->pos;
    int rv = output_cbufd(cb, ob, false);
    if (rv == 0 && ob->pos == pos && !has_anno) {
      ob->pos = 0;
    }
    return rv;
  }
  return output_cbuf(cb, ob);
}


static int cb_serialize(lua_State *lua)
{
  lsb_output_buffer *ob = lua_touserdata(lua, -1);
  const char *key = lua_touserdata(lua, -2);
  circular_buffer *cb = lua_touserdata(lua, -3);
  if (!(ob && key && cb)) {return 1;}
  if (lsb_outputf(ob,
                  "if %s == nil then "
                  "%s = circular_buffer.new(%d, %d, %d) end\n",
                  key,
                  key,
                  cb->rows,
                  cb->columns,
                  cb->seconds_per_row)) {
    return 1;
  }

  unsigned col;
  for (col = 0; col < cb->columns; ++col) {
    if (lsb_outputf(ob, "%s:set_header(%d, \"%s\", \"%s\", \"%s\")\n",
                    key,
                    col + 1,
                    cb->headers[col].name,
                    cb->headers[col].unit,
                    agg_methods[cb->headers[col].aggregation])) {
      return 1;
    }
  }

  if (lsb_outputf(ob, "%s:fromstring(\"%lld %d",
                  key,
                  (long long)cb->current_time,
                  cb->current_row)) {
    return 1;
  }

  for (unsigned row = 0; row < cb->rows; ++row) {
    for (col = 0; col < cb->columns; ++col) {
      if (lsb_outputc(ob, ' ')) return 1;
      // intentionally not serialized as Lua
      if (lsb_outputd(ob,
                      cb->values[(row * cb->tcolumns) + col * 2])) {
        return 1;
      }
    }
  }
  if (lsb_outputc(ob, ' ')) return 1;
  if (output_cbufd(cb, ob, true)) {return 1;}
  if (ob->buf[ob->pos - 1] == ' ') {
    --ob->pos;
  }
  if (lsb_outputs(ob, "\")\n", 3)) {return 1;}
  if (output_annotations(lua, cb, ob, key)) return 1;
  return 0;
}


static int cb_gc(lua_State *lua)
{
  circular_buffer *cb = check_circular_buffer(lua, 0);
  if (cb->ref != LUA_NOREF) {
    lua_getglobal(lua, mozsvc_circular_buffer_table);
    if (lua_istable(lua, -1)) {
      luaL_unref(lua, -1, cb->ref);
    }
    lua_pop(lua, 1);
    cb->ref = LUA_NOREF;
  }
  return 0;
}

#else
static int cb_reset_delta(lua_State *lua)
{
  circular_buffer *cb = check_circular_buffer(lua, 0);
  for (unsigned row = 0; row < cb->rows; ++row) {
    for (unsigned col = 0; col < cb->columns; ++col) {
      int idx = (row * cb->tcolumns) + col * 2 + 1;
      cb->values[idx] = NAN;
    }
  }
  return 0;
}
#endif

static const struct luaL_reg circular_bufferlib_f[] =
{
  { "new", cb_new },
  { "version", cb_version },
  { NULL, NULL }
};

static const struct luaL_reg circular_bufferlib_m[] =
{
  { "add", cb_add },
  { "get", cb_get },
  { "get_configuration", cb_get_configuration },
  { "current_time", cb_current_time },
  { "get_header", cb_get_header },
  { "get_range", cb_get_range },
  { "set", cb_set },
  { "set_header", cb_set_header },
  // @todo add __tostring for non sandbox use

#ifdef LUA_SANDBOX
  { "annotate", cb_annotate },
  { "format", cb_format },
  { "fromstring", cb_fromstring }, // used for sandbox data restoration
  { "__gc", cb_gc },
#else
  { "reset_delta", cb_reset_delta },
#endif
  { NULL, NULL }
};


int luaopen_circular_buffer(lua_State *lua)
{
#ifdef LUA_SANDBOX
  lua_newtable(lua);
  lsb_add_serialize_function(lua, cb_serialize);
  lsb_add_output_function(lua, cb_output);
  lua_replace(lua, LUA_ENVIRONINDEX);
#endif
  luaL_newmetatable(lua, mozsvc_circular_buffer);
  lua_pushvalue(lua, -1);
  lua_setfield(lua, -2, "__index");
  luaL_register(lua, NULL, circular_bufferlib_m);

  lua_pushinteger(lua, 1); // offset to allow reuse of cb_get with deltas
  lua_pushcclosure(lua, cb_get, 1);
  lua_setfield(lua, -2, "get_delta");

  lua_pushinteger(lua, 1);  // offset to allow reuse of cb_get_range with deltas
  lua_pushcclosure(lua, cb_get_range, 1);
  lua_setfield(lua, -2, "get_range_delta");

  luaL_register(lua, mozsvc_circular_buffer_table, circular_bufferlib_f);
  return 1;
}