📄 simplecache.cc
字号:
/* * simplecache.cc * Copyright (C) 2000 by the University of Southern California * $Id: simplecache.cc,v 1.2 2005/08/25 18:58:05 johnh Exp $ * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License, * version 2, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. * * * The copyright of this module includes the following * linking-with-specific-other-licenses addition: * * In addition, as a special exception, the copyright holders of * this module give you permission to combine (via static or * dynamic linking) this module with free software programs or * libraries that are released under the GNU LGPL and with code * included in the standard release of ns-2 under the Apache 2.0 * license or under otherwise-compatible licenses with advertising * requirements (or modified versions of such code, with unchanged * license). You may copy and distribute such a system following the * terms of the GNU GPL for this module and the licenses of the * other code concerned, provided that you include the source code of * that other code when and as the GNU GPL requires distribution of * source code. * * Note that people who make modified versions of this module * are not obligated to grant this special exception for their * modified versions; it is their choice whether to do so. The GNU * General Public License gives permission to release a modified * version without this exception; this exception also makes it * possible to release a modified version which carries forward this * exception. * *///// Other copyrights might apply to parts of this software and are so// noted when applicable./* simplecache.cc cache used in the mobicom 98 submission. see the paper for a description Ported from CMU/Monarch's code, appropriate copyright applies. */#ifdef DSR_SIMPLECACHEextern "C" {#include <stdio.h>#include <stdarg.h>};#undef DEBUG#include <god.h>#include "path.h"#include "routecache.h"#ifdef DSR_CACHE_STATS#include "cache_stats.h"#endif/* invariants - no path contains an id more than once - all paths start with the id of our node (MAC_id or net_id)*/#define fout stdoutstatic const int verbose = 0;static const int verbose_debug = 0;/*=============================================================== function selectors----------------------------------------------------------------*/bool cache_ignore_hints = false; // ignore all hints?bool cache_use_overheard_routes = true; // if we are A, and we over hear a rt Z Y (X) W V U, do we add route// A X W V U to the cache?/*=============================================================== Class declaration----------------------------------------------------------------*/class MobiCache;class Cache {friend class MobiCache;public: Cache(char *name, int size, MobiCache *rtcache); ~Cache(); int pickVictim(int exclude = -1); // returns the index of a suitable victim in the cache // will spare the life of exclude bool searchRoute(const ID& dest, int& i, Path &path, int &index); // look for dest in cache, starting at index, //if found, rtn true with path s.t. cache[index] == path && path[i] == dest Path* addRoute(Path &route, int &prefix_len); // rtns a pointer the path in the cache that we added void noticeDeadLink(const ID&from, const ID& to); // the link from->to isn't working anymore, purge routes containing // it from the cacheprivate: Path *cache; int size; int victim_ptr; // next victim for eviction MobiCache *routecache; char *name;};///////////////////////////////////////////////////////////////////////////class MobiCache : public RouteCache {friend class Cache;friend class MobiHandler;public: MobiCache(const ID& MAC_id, const ID& net_id,int psize = 30,int ssize = 34 ); MobiCache(); ~MobiCache(); void noticeDeadLink(const ID&from, const ID& to, Time t); // the link from->to isn't working anymore, purge routes containing // it from the cache void noticeRouteUsed(const Path& route, Time t, const ID& who_from); // tell the cache about a route we saw being used void addRoute(const Path& route, Time t, const ID& who_from); // add this route to the cache (presumably we did a route request // to find this route and don't want to lose it) bool findRoute(ID dest, Path& route, int for_use = 0); // if there is a cached path from us to dest returns true and fills in // the route accordingly. returns false otherwise // if for_use, then we assume that the node really wants to keep // the returned route so it will be promoted to primary storage if not there // already int command(int argc, const char*const* argv);protected: Cache *secondary_cache; /* routes we've learned via a speculative process that might not pan out */#ifdef DSR_CACHE_STATS void periodic_checkCache(void); void checkRoute(Path *p, int action, int prefix_len); void checkRoute(Path &p, int&, int&, double&, int&, int&, double &);#endif};RouteCache *makeRouteCache(){ return new MobiCache();}/*=============================================================== OTcl definition----------------------------------------------------------------*/static class MobiCacheClass : public TclClass {public: MobiCacheClass() : TclClass("MobiCache") {} TclObject* create(int, const char*const*) { return (new MobiCache); }} class_MobiCache;/*=============================================================== Constructors----------------------------------------------------------------*/MobiCache::MobiCache(): RouteCache(){ secondary_cache = new Cache("secondary", 10000, this); assert(secondary_cache != NULL);#ifdef DSR_CACHE_STATS stat.reset();#endif}MobiCache::~MobiCache(){ delete secondary_cache;}intMobiCache::command(int argc, const char*const* argv){ if(argc == 2 && strcasecmp(argv[1], "startdsr") == 0) { if (ID(1,::IP) == net_id) trace("Sconfig %.5f using MOBICACHE", Scheduler::instance().clock()); // FALL-THROUGH } return RouteCache::command(argc, argv);}#ifdef DSR_CACHE_STATSvoidMobiCache::periodic_checkCache(){ int c; int route_count = 0; int route_bad_count = 0; int subroute_count = 0; int subroute_bad_count = 0; int link_bad_count = 0; double link_bad_time = 0.0; int link_bad_tested = 0; int link_good_tested = 0; for(c = 0; c < secondary_cache->size; c++) { int x = 0; if (secondary_cache->cache[c].length() == 0) continue; checkRoute(secondary_cache->cache[c], x, link_bad_count, link_bad_time, link_bad_tested, link_good_tested, stat.link_good_time); route_count += 1; route_bad_count += x ? 1 : 0; subroute_count += secondary_cache->cache[c].length() - 1; subroute_bad_count += x; } // lifetime of good link is (total time) / (total num links - num bad links) stat.link_good_time = stat.link_good_time / (subroute_count - link_bad_count); trace("SRC %.9f _%s_ cache-summary %d %d %d %d | %d %.9f %d %d | %d %d %d %d %d | %d %d %d %d %d | %d %d %d %d %d %d %.9f", Scheduler::instance().clock(), net_id.dump(), route_count, route_bad_count, subroute_count, subroute_bad_count, link_bad_count, link_bad_count ? link_bad_time/link_bad_count : 0.0, link_bad_tested, link_good_tested, stat.route_add_count, stat.route_add_bad_count, stat.subroute_add_count, stat.subroute_add_bad_count, stat.link_add_tested, stat.route_notice_count, stat.route_notice_bad_count, stat.subroute_notice_count, stat.subroute_notice_bad_count, stat.link_notice_tested, stat.route_find_count, stat.route_find_for_me, stat.route_find_bad_count, stat.route_find_miss_count, stat.subroute_find_count, stat.subroute_find_bad_count, stat.link_good_time); stat.reset();}#endif /* DSR_CACHE_STATS *//*=============================================================== member functions----------------------------------------------------------------*/voidMobiCache::addRoute(const Path& route, Time t, const ID& who_from)// add this route to the cache (presumably we did a route request// to find this route and don't want to lose it)// who_from is the id of the routes provider{ Path rt; if(pre_addRoute(route, rt, t, who_from) == 0) return; // must call addRoute before checkRoute int prefix_len = 0;#ifdef DSR_CACHE_STATS Path *p = secondary_cache->addRoute(rt, prefix_len); checkRoute(p, ACTION_ADD_ROUTE, prefix_len);#else (void) secondary_cache->addRoute(rt, prefix_len);#endif}voidMobiCache::noticeDeadLink(const ID&from, const ID& to, Time) // the link from->to isn't working anymore, purge routes containing // it from the cache{ if(verbose_debug) trace("SRC %.9f _%s_ dead link %s->%s", Scheduler::instance().clock(), net_id.dump(), from.dump(), to.dump()); secondary_cache->noticeDeadLink(from, to); return;}voidMobiCache::noticeRouteUsed(const Path& p, Time t, const ID& who_from)// tell the cache about a route we saw being used{ Path stub; if(pre_noticeRouteUsed(p, stub, t, who_from) == 0) return; int prefix_len = 0;#ifdef DSR_CACHE_STATS Path *p0 = secondary_cache->addRoute(stub, prefix_len); checkRoute(p0, ACTION_NOTICE_ROUTE, prefix_len);#else (void) secondary_cache->addRoute(stub, prefix_len);#endif}boolMobiCache::findRoute(ID dest, Path& route, int for_me)// if there is a cached path from us to dest returns true and fills in// the route accordingly. returns false otherwise// if for_me, then we assume that the node really wants to keep // the returned route so it will be promoted to primary storage if not there// already{ Path path; int min_index = -1; int min_length = MAX_SR_LEN + 1; int min_cache = 0; // 2 == primary, 1 = secondary int index; int len; assert(!(net_id == invalid_addr)); index = 0; while (secondary_cache->searchRoute(dest, len, path, index)) { if (len < min_length) { min_index = index; min_cache = 1; min_length = len; route = path; } index++; } if (min_cache) { route.setLength(min_length + 1); if (verbose_debug) trace("SRC %.9f _%s_ $hit for %s in %s %s", Scheduler::instance().clock(), net_id.dump(),
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -