#include "stream.h" #include static void reset_report(struct report *r) { memset(r, 0, sizeof(*r)); r->role = -1; for (int c = 0; c < CHANNEL_LIMIT; ++c) { r->channels[c].last_tick = -1; } } static int configuration_valid(const struct job *j) { if (j == NULL) { return 0; } if (j->serial < 0 || j->serial > 1000000) { return 0; } if (j->count < 0 || j->count > ROW_LIMIT) { return 0; } if (j->channels < 1 || j->channels > CHANNEL_LIMIT) { return 0; } if (j->lower < -1000 || j->lower > 1000) { return 0; } if (j->upper < j->lower || j->upper > 1000) { return 0; } if (j->flags < 0 || j->flags > 1) { return 0; } return 1; } static int record_valid(const struct job *j, const struct record *v) { if (v->kind < 0 || v->kind > 2) { return 0; } if (v->tick < 0 || v->tick > 65535) { return 0; } if (v->value < -32768 || v->value > 32767) { return 0; } if (v->kind == 2) { return v->channel == 0 && v->value == 0; } if (v->channel < 0 || v->channel >= j->channels) { return 0; } return 1; } static int classify(const struct job *j, const struct record *v) { if (!record_valid(j, v)) { return 3; } if (v->kind == 1) { return 4; } if (v->kind == 2) { return 5; } if (v->value < j->lower || v->value > j->upper) { return 2; } return 1; } static void account_trend(struct channel *c, const struct record *v) { if (c->accepted == 0) { c->first_tick = v->tick; c->low_tick = v->tick; c->high_tick = v->tick; c->previous = v->value; c->run = 1; c->longest_run = 1; return; } if (v->tick < c->low_tick) { c->low_tick = v->tick; } if (v->tick > c->high_tick) { c->high_tick = v->tick; } long long elapsed = (long long)v->tick - c->last_tick; c->intervals += elapsed; if (elapsed < 0) { c->regressions += 1; } long long difference = (long long)v->value - c->previous; if (difference > 0) { c->rising += 1; c->movement += difference; c->run = 1; } else if (difference < 0) { c->falling += 1; c->movement -= difference; c->run = 1; } else { c->repeated += 1; c->run += 1; } if (c->run > c->longest_run) { c->longest_run = c->run; } c->previous = v->value; } static int format_channel(struct text *t, const struct job *j, int role, const struct channel *c) { text_reset(t); if (text_word(t, "S") != 0) { return -1; } if (text_field(t, j->serial) != 0) { return -1; } if (text_field(t, role) != 0) { return -1; } if (text_field(t, c->accepted) != 0) { return -1; } if (text_field(t, c->sum) != 0) { return -1; } if (text_field(t, c->movement) != 0) { return -1; } if (text_field(t, c->regressions) != 0) { return -1; } if (text_field(t, c->longest_run) != 0) { return -1; } return text_end(t); } static void accept_sample(struct report *r, const struct record *v, int ordinal) { struct channel *c = &r->channels[v->channel]; account_trend(c, v); if (c->accepted == 0) { c->minimum = v->value; c->maximum = v->value; } else { if (v->value < c->minimum) { c->minimum = v->value; } if (v->value > c->maximum) { c->maximum = v->value; } } c->accepted += 1; c->sum += v->value; c->last_tick = v->tick; r->accepted += 1; r->sum += v->value; unsigned term = (unsigned)(v->value + 32768); term = term * 31u + (unsigned)v->tick; term = term * 17u + (unsigned)v->channel; term += (unsigned)ordinal; r->checksum = (r->checksum + term) % 1000003u; } static void account(struct report *r, const struct record *v, int ordinal, int status) { switch (status) { case 1: accept_sample(r, v, ordinal); break; case 2: r->filtered += 1; r->channels[v->channel].filtered += 1; break; case 3: r->invalid += 1; break; case 4: r->notes += 1; r->channels[v->channel].notes += 1; break; case 5: r->barriers += 1; break; default: break; } } static struct text *reserve_line(struct plan *p, int role) { if (p->count >= PLAN_LIMIT) { return NULL; } struct line *line = &p->lines[p->count]; line->role = role; text_reset(&line->text); p->count += 1; return &line->text; } static int data_line(struct plan *p, const struct job *j, int ordinal, int status) { struct text *t = reserve_line(p, 0); if (t == NULL) { return -1; } if (status == 1) { return format_data(t, j, ordinal, &j->rows[ordinal]); } return format_barrier(t, j, ordinal, j->rows[ordinal].tick); } static int audit_line(struct plan *p, const struct job *j, int ordinal, int status) { struct text *t = reserve_line(p, 1); if (t == NULL) { return -1; } return format_audit(t, j, ordinal, status); } static int index_line(struct plan *p, const struct job *j, int ordinal, size_t offset, size_t length) { struct text *t = reserve_line(p, 2); if (t == NULL) { return -1; } return format_index(t, j, ordinal, offset, length); } static int append_record(struct plan *p, const struct job *j, int ordinal, int status, size_t *offset) { size_t length = 0; if (status == 1 || status == 5) { if (data_line(p, j, ordinal, status) != 0) { return -1; } length = p->lines[p->count - 1].text.length; } if (audit_line(p, j, ordinal, status) != 0) { return -1; } if ((j->flags & 1) && length != 0) { if (index_line(p, j, ordinal, *offset, length) != 0) { return -1; } } *offset += length; return 0; } static int append_trailers(struct plan *p, const struct job *j, const struct report *r, size_t offset) { struct text *t = reserve_line(p, 0); if (t == NULL || format_total(t, j, r) != 0) { return -1; } offset += t->length; t = reserve_line(p, 1); if (t == NULL || format_end(t, j, 0, offset) != 0) { return -1; } if (j->flags & 1) { t = reserve_line(p, 2); if (t == NULL || format_end(t, j, 1, offset) != 0) { return -1; } } return 0; } int build_plan(const struct job *j, struct plan *p, struct report *r) { reset_report(r); memset(p, 0, sizeof(*p)); if (!configuration_valid(j)) { r->error = 1; return -1; } int statuses[ROW_LIMIT]; for (int i = 0; i < j->count; ++i) { statuses[i] = classify(j, &j->rows[i]); account(r, &j->rows[i], i, statuses[i]); } size_t offset = 0; for (int i = 0; i < j->count; ++i) { if (append_record(p, j, i, statuses[i], &offset) != 0) { r->error = 2; return -1; } } for (int c = 0; c < j->channels; ++c) { struct text *t = reserve_line(p, 1); if (t == NULL || format_channel(t, j, c, &r->channels[c]) != 0) { r->error = 2; return -1; } } if (append_trailers(p, j, r, offset) != 0) { r->error = 2; return -1; } for (int i = 0; i < p->count; ++i) { int role = p->lines[i].role; r->planned[role] += p->lines[i].text.length; } return 0; }