📄 aggregatelast.cc
字号:
/* * aggregatelast.{cc,hh} -- output the last packet per aggregate * Eddie Kohler * * Copyright (c) 2002 International Computer Science Institute * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, subject to the conditions * listed in the Click LICENSE file. These conditions include: you must * preserve this copyright notice, and you cannot mention the copyright * holders in advertising related to the Software without their permission. * The Software is provided WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED. This * notice is a summary of the Click LICENSE file; the license in that file is * legally binding. */#include <click/config.h>#include "aggregatelast.hh"#include <click/confparse.hh>#include <click/error.hh>#include <click/packet_anno.hh>#include <click/router.hh>CLICK_DECLSAggregateLast::AggregateLast() : Element(1, 1), _agg_notifier(0), _clear_task(this), _needs_clear(0){ memset(_packets, 0, sizeof(_packets)); memset(_counts, 0, sizeof(_counts));}AggregateLast::~AggregateLast(){}voidAggregateLast::notify_noutputs(int n){ set_noutputs(n <= 1 ? 1 : 2);}intAggregateLast::configure(Vector<String> &conf, ErrorHandler *errh){ Element *e = 0; _stop_after_clear = false; if (cp_va_parse(conf, this, errh, cpKeywords, "NOTIFIER", cpElement, "aggregate creation and deletion notifier", &e, "STOP_AFTER_CLEAR", cpBool, "stop router after 'clear' completes?", &_stop_after_clear, cpEnd) < 0) return -1; if (e && !(_agg_notifier = (AggregateNotifier *)e->cast("AggregateNotifier"))) return errh->error("%s is not an AggregateNotifier", e->id().cc()); return 0;}intAggregateLast::initialize(ErrorHandler *){ if (_agg_notifier) _agg_notifier->add_listener(this); _clear_task.initialize(this, false); return 0;}voidAggregateLast::cleanup(CleanupStage){ for (int i = 0; i < NPLANE; i++) { if (Packet ***p = _packets[i]) { for (int j = 0; j < NCOL; j++) if (Packet **q = p[j]) { for (int k = 0; k < NROW; k++) if (q[k]) q[k]->kill(); delete[] q; } delete[] p; } delete[] _counts[i]; }}Packet **AggregateLast::create_row(uint32_t agg){ int planeno = (agg >> PLANE_SHIFT) & PLANE_MASK; if (!_packets[planeno]) { if (!(_packets[planeno] = new Packet **[NCOL])) return 0; memset(_packets[planeno], 0, sizeof(Packet **) * NCOL); if (!_agg_notifier) /* skip */; else if (!(_counts[planeno] = new uint32_t[NCOL + 1])) { delete[] _packets[planeno]; _packets[planeno] = 0; return 0; } else memset(_counts[planeno], 0, sizeof(uint32_t) * (NCOL + 1)); } Packet ***plane = _packets[planeno]; int colno = (agg >> COL_SHIFT) & COL_MASK; if (!plane[colno]) { if (!(plane[colno] = new Packet *[NROW])) return 0; memset(plane[colno], 0, sizeof(Packet *) * NROW); } return plane[colno];}voidAggregateLast::push(int, Packet *p){ // clean if we should clean if (_clear_task.scheduled()) { _clear_task.unschedule(); run_scheduled(); } uint32_t agg = AGGREGATE_ANNO(p); if (Packet **r = row(agg)) { static_assert(ROW_SHIFT == 0); r += (agg & ROW_MASK); if (*r) { SET_EXTRA_PACKETS_ANNO(p, EXTRA_PACKETS_ANNO(p) + 1 + EXTRA_PACKETS_ANNO(*r)); SET_EXTRA_LENGTH_ANNO(p, EXTRA_LENGTH_ANNO(p) + (*r)->length() + EXTRA_LENGTH_ANNO(*r)); SET_FIRST_TIMESTAMP_ANNO(p, FIRST_TIMESTAMP_ANNO(*r)); checked_output_push(1, *r); } else SET_FIRST_TIMESTAMP_ANNO(p, p->timestamp_anno()); *r = p; }}voidAggregateLast::aggregate_notify(uint32_t agg, AggregateEvent event, const Packet *){ int plane = (agg >> PLANE_SHIFT) & PLANE_MASK; int col = (agg >> COL_SHIFT) & COL_MASK; Packet **r = row(agg); if (!r) // out of memory return; r += (agg & ROW_MASK); if (event == NEW_AGG) { if ((++_counts[plane][col]) == 1) _counts[plane][NCOL]++; } else if (event == DELETE_AGG && *r) { // XXX should we push in a notify function? Well why not. output(0).push(*r); *r = 0; if ((--_counts[plane][col]) == 0) { // get rid of empty row delete[] _packets[plane][col]; _packets[plane][col] = 0; // get rid of empty column if ((--_counts[plane][NCOL]) == 0) { delete[] _counts[plane]; _counts[plane] = 0; delete[] _packets[plane]; _packets[plane] = 0; } } }}boolAggregateLast::run_task(){ if (!_needs_clear) return false; _needs_clear = 0; // may take a long time! for (int i = 0; i < NPLANE; i++) if (Packet ***p = _packets[i]) { for (int j = 0; j < NCOL; j++) if (Packet **q = p[j]) { for (int k = 0; k < NROW; k++) if (Packet *r = q[k]) output(0).push(r); delete[] q; } delete[] p; if (_agg_notifier) delete[] _counts[i]; } memset(_packets, 0, sizeof(_packets)); memset(_counts, 0, sizeof(_counts)); if (_stop_after_clear) router()->please_stop_driver(); return true;}enum { H_CLEAR };intAggregateLast::write_handler(const String &, Element *e, void *thunk, ErrorHandler *){ AggregateLast *al = reinterpret_cast<AggregateLast *>(e); switch (reinterpret_cast<intptr_t>(thunk)) { case H_CLEAR: al->_needs_clear = 1; al->_clear_task.reschedule(); break; } return 0;}voidAggregateLast::add_handlers(){ add_write_handler("clear", write_handler, (void *)H_CLEAR);}ELEMENT_REQUIRES(userlevel AggregateNotifier)EXPORT_ELEMENT(AggregateLast)CLICK_ENDDECLS
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -