File: ts_lua_stat.c

package info (click to toggle)
trafficserver 9.2.5%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 53,008 kB
  • sloc: cpp: 345,484; ansic: 31,134; python: 24,200; sh: 7,271; makefile: 3,045; perl: 2,261; java: 277; pascal: 119; sql: 94; xml: 2
file content (249 lines) | stat: -rw-r--r-- 6,071 bytes parent folder | download | duplicates (4)
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
/*
  Licensed to the Apache Software Foundation (ASF) under one
  or more contributor license agreements.  See the NOTICE file
  distributed with this work for additional information
  regarding copyright ownership.  The ASF licenses this file
  to you under the Apache License, Version 2.0 (the
  "License"); you may not use this file except in compliance
  with the License.  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/

#include "ts_lua_util.h"

typedef enum {
  TS_LUA_STAT_PERSISTENT     = TS_STAT_PERSISTENT,
  TS_LUA_STAT_NON_PERSISTENT = TS_STAT_NON_PERSISTENT
} TSLuaStatPersistentType;

static const ts_lua_var_item ts_lua_stat_persistent_vars[] = {
  TS_LUA_MAKE_VAR_ITEM(TS_LUA_STAT_PERSISTENT),
  TS_LUA_MAKE_VAR_ITEM(TS_LUA_STAT_NON_PERSISTENT),
};

typedef enum {
  TS_LUA_STAT_SYNC_SUM     = TS_STAT_SYNC_SUM,
  TS_LUA_STAT_SYNC_COUNT   = TS_STAT_SYNC_COUNT,
  TS_LUA_STAT_SYNC_AVG     = TS_STAT_SYNC_AVG,
  TS_LUA_STAT_SYNC_TIMEAVG = TS_STAT_SYNC_TIMEAVG
} TSLuaStatSyncType;

static const ts_lua_var_item ts_lua_stat_sync_vars[] = {
  TS_LUA_MAKE_VAR_ITEM(TS_LUA_STAT_SYNC_SUM),
  TS_LUA_MAKE_VAR_ITEM(TS_LUA_STAT_SYNC_COUNT),
  TS_LUA_MAKE_VAR_ITEM(TS_LUA_STAT_SYNC_AVG),
  TS_LUA_MAKE_VAR_ITEM(TS_LUA_STAT_SYNC_TIMEAVG),
};

typedef enum {
  TS_LUA_RECORDDATATYPE_INT = TS_RECORDDATATYPE_INT,
} TSLuaStatRecordType;

static const ts_lua_var_item ts_lua_stat_record_vars[] = {
  TS_LUA_MAKE_VAR_ITEM(TS_LUA_RECORDDATATYPE_INT),
};

static void ts_lua_inject_stat_variables(lua_State *L);

static int ts_lua_stat_create(lua_State *L);
static int ts_lua_stat_find(lua_State *L);

static int ts_lua_stat_increment(lua_State *L);
static int ts_lua_stat_decrement(lua_State *L);
static int ts_lua_stat_get_value(lua_State *L);
static int ts_lua_stat_set_value(lua_State *L);

void
ts_lua_inject_stat_api(lua_State *L)
{
  ts_lua_inject_stat_variables(L);

  lua_pushcfunction(L, ts_lua_stat_create);
  lua_setfield(L, -2, "stat_create");

  lua_pushcfunction(L, ts_lua_stat_find);
  lua_setfield(L, -2, "stat_find");
}

static void
ts_lua_inject_stat_variables(lua_State *L)
{
  size_t i;

  for (i = 0; i < sizeof(ts_lua_stat_persistent_vars) / sizeof(ts_lua_var_item); i++) {
    lua_pushinteger(L, ts_lua_stat_persistent_vars[i].nvar);
    lua_setglobal(L, ts_lua_stat_persistent_vars[i].svar);
  }

  for (i = 0; i < sizeof(ts_lua_stat_sync_vars) / sizeof(ts_lua_var_item); i++) {
    lua_pushinteger(L, ts_lua_stat_sync_vars[i].nvar);
    lua_setglobal(L, ts_lua_stat_sync_vars[i].svar);
  }

  for (i = 0; i < sizeof(ts_lua_stat_record_vars) / sizeof(ts_lua_var_item); i++) {
    lua_pushinteger(L, ts_lua_stat_record_vars[i].nvar);
    lua_setglobal(L, ts_lua_stat_record_vars[i].svar);
  }
}

static int
ts_lua_stat_create(lua_State *L)
{
  const char *name;
  size_t name_len;
  int type;
  int persist;
  int sync;
  int idp;

  name = luaL_checklstring(L, 1, &name_len);

  if (lua_isnil(L, 2)) {
    type = TS_RECORDDATATYPE_INT;
  } else {
    type = luaL_checkinteger(L, 2);
  }

  if (lua_isnil(L, 3)) {
    persist = TS_STAT_PERSISTENT;
  } else {
    persist = luaL_checkinteger(L, 3);
  }

  if (lua_isnil(L, 4)) {
    sync = TS_STAT_SYNC_SUM;
  } else {
    sync = luaL_checkinteger(L, 4);
  }

  if (name && name_len) {
    if (TSStatFindName(name, &idp) == TS_ERROR) {
      idp = TSStatCreate(name, type, persist, sync);
    }

    lua_newtable(L);
    lua_pushnumber(L, idp);
    lua_setfield(L, -2, "id");

    lua_pushcfunction(L, ts_lua_stat_increment);
    lua_setfield(L, -2, "increment");
    lua_pushcfunction(L, ts_lua_stat_decrement);
    lua_setfield(L, -2, "decrement");
    lua_pushcfunction(L, ts_lua_stat_get_value);
    lua_setfield(L, -2, "get_value");
    lua_pushcfunction(L, ts_lua_stat_set_value);
    lua_setfield(L, -2, "set_value");
  } else {
    lua_pushnil(L);
  }

  return 1;
}

static int
ts_lua_stat_find(lua_State *L)
{
  const char *name;
  size_t name_len;
  int idp;

  name = luaL_checklstring(L, 1, &name_len);

  if (name && name_len) {
    if (TSStatFindName(name, &idp) != TS_ERROR) {
      lua_newtable(L);
      lua_pushnumber(L, idp);
      lua_setfield(L, -2, "id");

      lua_pushcfunction(L, ts_lua_stat_increment);
      lua_setfield(L, -2, "increment");
      lua_pushcfunction(L, ts_lua_stat_decrement);
      lua_setfield(L, -2, "decrement");
      lua_pushcfunction(L, ts_lua_stat_get_value);
      lua_setfield(L, -2, "get_value");
      lua_pushcfunction(L, ts_lua_stat_set_value);
      lua_setfield(L, -2, "set_value");
    } else {
      lua_pushnil(L);
    }
  } else {
    lua_pushnil(L);
  }

  return 1;
}

static int
ts_lua_stat_increment(lua_State *L)
{
  int increment;
  int idp;
  increment = luaL_checkinteger(L, 2);

  luaL_checktype(L, 1, LUA_TTABLE);
  lua_getfield(L, -2, "id");
  idp = luaL_checknumber(L, -1);
  lua_pop(L, 1);

  TSStatIntIncrement(idp, increment);

  return 0;
}

static int
ts_lua_stat_decrement(lua_State *L)
{
  int decrement;
  int idp;
  decrement = luaL_checkinteger(L, 2);

  luaL_checktype(L, 1, LUA_TTABLE);
  lua_getfield(L, -2, "id");
  idp = luaL_checknumber(L, -1);
  lua_pop(L, 1);

  TSStatIntDecrement(idp, decrement);

  return 0;
}

static int
ts_lua_stat_get_value(lua_State *L)
{
  int value;
  int idp;

  luaL_checktype(L, 1, LUA_TTABLE);
  lua_getfield(L, -1, "id");
  idp = luaL_checknumber(L, -1);
  lua_pop(L, 1);

  value = TSStatIntGet(idp);

  lua_pushnumber(L, value);
  return 1;
}

static int
ts_lua_stat_set_value(lua_State *L)
{
  int value;
  int idp;
  value = luaL_checkinteger(L, 2);

  luaL_checktype(L, 1, LUA_TTABLE);
  lua_getfield(L, -2, "id");
  idp = luaL_checknumber(L, -1);
  lua_pop(L, 1);

  TSStatIntSet(idp, value);

  return 0;
}