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
|
/*
* Copyright (c) Xerox Corporation 1998. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* 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.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* Linking this file statically or dynamically with other modules is making
* a combined work based on this file. Thus, the terms and conditions of
* the GNU General Public License cover the whole combination.
*
* In addition, as a special exception, the copyright holders of this file
* give you permission to combine this file 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
* file 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 file 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.
*
* $Header: /cvsroot/nsnam/ns-2/webcache/pagepool.h,v 1.17 2005/09/18 23:33:35 tomh Exp $
*
*/
//
// Definitions for class PagePool
//
#ifndef ns_pagepool_h
#define ns_pagepool_h
#include <stdio.h>
#include <limits.h>
#include <tcl.h>
#include <ranvar.h>
#include <tclcl.h>
#include "config.h"
enum WebPageType { HTML, MEDIA };
class Page {
public:
Page(int size) : size_(size) {}
virtual ~Page () {}
int size() const { return size_; }
int& id() { return id_; }
virtual WebPageType type() const = 0; // Page type: HTML or MEDIA
protected:
int size_;
int id_;
};
class ServerPage : public Page {
public:
ServerPage(int size, int id) : Page(size) {
id_ = id, mtime_ = NULL, num_mtime_ = 0;
}
virtual ~ServerPage() {
if (mtime_ != NULL)
delete []mtime_;
}
virtual WebPageType type() const { return HTML; }
int& size() { return size_; }
int& mtime(int n) { return mtime_[n]; }
int& num_mtime() { return num_mtime_; }
void set_mtime(int *mt, int n);
protected:
int *mtime_;
int num_mtime_;
};
class HttpApp;
// Page states
const int HTTP_PAGE_STATE_MASK = 0x00FF;
const int HTTP_ALL_PAGE_STATES = 0x00ff; // all page states
const int HTTP_VALID_PAGE = 0x01; // Valid page
const int HTTP_SERVER_DOWN = 0x02; // Server is down. Don't know if
// page is valid
const int HTTP_VALID_HEADER = 0x04; // Only meta-data is valid
const int HTTP_UNREAD_PAGE = 0x08; // Unread valid page
// Used only for server to manage its pages. A none-cacheable page won't be
// stored by caches and clients.
const int HTTP_UNCACHEABLE = 0x10; // Non-cacheable page
// Page actions
const int HTTP_PAGE_ACTION_MASK = 0xFF00; // Page action bit mask
const int HTTP_MANDATORY_PUSH = 0x1000; // If the page is mandatory pushed
struct PageID {
PageID() : s_(NULL), id_(0) {}
PageID(int *buffer) {
PageID *realBuffer = reinterpret_cast <PageID *> (buffer);
s_ = realBuffer->s_;
id_ = realBuffer->id_;
}
PageID(HttpApp *app, int id) {
s_ = app;
id_ = id;
}
HttpApp* s_;
int id_;
};
class ClientPage : public Page {
public:
ClientPage(const char *n, int s, double mt, double et, double a);
virtual ~ClientPage() {}
virtual WebPageType type() const { return HTML; }
virtual void print_info(char* buf);
void name(char* buf);
double& mtime() { return mtime_; }
double& etime() { return etime_; }
double& age() { return age_; }
HttpApp* server() { return server_; }
// Page becomes valid. Clear all other possible invalid bits
void validate(double mtime) {
if (mtime_ >= mtime)
abort(); // This shouldn't happen!
// Clear server down bit
clear_page_state(HTTP_SERVER_DOWN);
set_page_state(HTTP_VALID_PAGE);
mtime_ = mtime;
}
void invalidate(double mtime) {
if (mtime_ >= mtime)
return;
clear_page_state(HTTP_VALID_PAGE);
clear_page_state(HTTP_VALID_HEADER);
mtime_ = mtime;
}
int is_valid() const {
return (status_ & HTTP_VALID_PAGE);
}
int is_header_valid() const {
return ((status_ & HTTP_VALID_PAGE) ||
(status_ & HTTP_VALID_HEADER));
}
inline void set_valid_hdr() {
// XXX page invalid, but only header valid
clear_page_state(HTTP_SERVER_DOWN);
clear_page_state(HTTP_VALID_PAGE);
set_page_state(HTTP_VALID_HEADER);
}
inline void set_uncacheable() {
set_page_state(HTTP_UNCACHEABLE);
}
inline int is_uncacheable() {
return (status_ & HTTP_UNCACHEABLE);
}
// Has nothing to do with valid/invalid/server_down etc. It can
// be combined with all other page status
inline void set_unread() {
set_page_state(HTTP_UNREAD_PAGE);
}
inline void set_read() {
clear_page_state(HTTP_UNREAD_PAGE);
}
inline int is_unread() { return (status_ & HTTP_UNREAD_PAGE); }
inline int is_server_down() { return (status_ & HTTP_SERVER_DOWN); }
inline void server_down() {
// Set page as invalid
// Don't change mtime, only change page status
clear_page_state(HTTP_VALID_PAGE);
clear_page_state(HTTP_VALID_HEADER);
set_page_state(HTTP_SERVER_DOWN);
}
// Flags to indicate whether we want to do all push or selective push
// If 0: selective push, otherwise all push
static int PUSHALL_;
inline int& counter() {
if (PUSHALL_) counter_ = INT_MAX;
return counter_;
}
inline int count_inval(int a, int th) {
if (PUSHALL_)
return INT_MAX;
else {
counter_ -= a;
if (counter_ < th)
counter_ = th;
return counter_;
}
}
inline int count_request(int b, int th) {
if (PUSHALL_)
return INT_MAX;
else {
counter_ += b;
if (counter_ > th)
counter_ = th;
return counter_;
}
}
inline void set_mpush(double time) {
set_page_action(HTTP_MANDATORY_PUSH), mpushTime_ = time;
}
inline void clear_mpush() { clear_page_action(HTTP_MANDATORY_PUSH); }
inline int is_mpush() { return status_ & HTTP_MANDATORY_PUSH; }
inline double mpush_time() { return mpushTime_; }
// Used to split page names into page identifiers
static void split_name(const char* name, PageID& id);
static void print_name(char* name, PageID& id);
protected:
void set_page_state(int state) {
status_ |= state;
}
void clear_page_state(int state) {
status_ = status_ & ~state;
}
void set_page_action(int action) {
status_ |= action;
}
void clear_page_action(int action) {
status_ = status_ & ~action;
}
HttpApp* server_;
double age_;
double mtime_; // modification time
double etime_; // entry time
int status_; // VALID or INVALID
int counter_; // counter for invalidation & request
double mpushTime_;
};
// Abstract page pool, used for interface only
class PagePool : public TclObject {
public:
PagePool() : num_pages_(0), start_time_(INT_MAX), end_time_(INT_MIN) {}
int num_pages() const { return num_pages_; }
protected:
virtual int command(int argc, const char*const* argv);
int num_pages_;
double start_time_;
double end_time_;
int duration_;
// Helper functions
TclObject* lookup_obj(const char* name) {
TclObject* obj = Tcl::instance().lookup(name);
if (obj == NULL)
fprintf(stderr, "Bad object name %s\n", name);
return obj;
}
};
// Page pool based on real server traces
const int TRACEPAGEPOOL_MAXBUF = 4096;
// This trace must contain web page names and all of its modification times
class TracePagePool : public PagePool {
public:
TracePagePool(const char *fn);
virtual ~TracePagePool();
virtual int command(int argc, const char*const* argv);
protected:
Tcl_HashTable *namemap_, *idmap_;
RandomVariable *ranvar_;
ServerPage* load_page(FILE *fp);
void change_time();
int add_page(const char* pgname, ServerPage *pg);
ServerPage* get_page(int id);
};
// Page pool based on mathematical models of request and page
// modification patterns
class MathPagePool : public PagePool {
public:
// XXX TBA: what should be here???
MathPagePool() : rvSize_(0), rvAge_(0) { num_pages_ = 1; }
protected:
virtual int command(int argc, const char*const* argv);
// Single page
RandomVariable *rvSize_;
RandomVariable *rvAge_;
};
// Assume one main page, which changes often, and multiple component pages
class CompMathPagePool : public PagePool {
public:
CompMathPagePool();
protected:
virtual int command(int argc, const char*const* argv);
RandomVariable *rvMainAge_; // modtime for main page
RandomVariable *rvCompAge_; // modtime for component pages
int main_size_, comp_size_;
};
class ClientPagePool : public PagePool {
public:
ClientPagePool();
virtual ~ClientPagePool();
virtual ClientPage* enter_page(int argc, const char*const* argv);
virtual ClientPage* enter_metadata(int argc, const char*const* argv);
virtual ClientPage* enter_page(const char *name, int size, double mt,
double et, double age);
virtual ClientPage* enter_metadata(const char *name, int size,
double mt, double et, double age);
virtual int remove_page(const char *name);
void invalidate_server(int server_id);
ClientPage* get_page(const char *name);
int get_mtime(const char *name, double &mt);
int set_mtime(const char *name, double mt);
int exist_page(const char *name) { return (get_page(name) != NULL); }
int get_size(const char *name, int &size);
int get_age(const char *name, double &age);
int get_etime(const char *name, double &et);
int set_etime(const char *name, double et);
int get_pageinfo(const char *name, char *buf);
virtual int command(int argc, const char*const* argv);
protected:
int add_page(ClientPage *pg);
Tcl_HashTable *namemap_;
};
// This is *not* designed for BU trace files. We should write a script to
// transform BU traces to a single trace file with the following format:
//
// <client id> <page id> <time> <size>
//
// Q: How would we deal with page size changes?
// What if simulated response time
// is longer and a real client request for the same page happened before the
// simulated request completes?
class ProxyTracePagePool : public PagePool {
public:
ProxyTracePagePool();
// : rvDyn_(NULL), rvStatic_(NULL), br_(0),
// size_(NULL), reqfile_(NULL), req_(NULL), lastseq_(0)
// {}
virtual ~ProxyTracePagePool();
virtual int command(int argc, const char*const* argv);
protected:
// How would we handle different types of page modifications? How
// to integrate bimodal, and multi-modal distributions?
int init_req(const char *fn);
int init_page(const char *fn);
int find_info();
RandomVariable *rvDyn_, *rvStatic_;
int br_; // bimodal ratio
int *size_; // page sizes
FILE *reqfile_; // request stream of proxy trace
struct ClientRequest {
ClientRequest() : seq_(0), nrt_(0), nurl_(0), fpos_(0)
{}
int seq_; // client sequence number, used to match
// client ids in the trace file
double nrt_; // next request time
int nurl_; // next request url
long fpos_; // position in file of its next request
};
Tcl_HashTable *req_; // Requests table
int nclient_, lastseq_;
ClientRequest* load_req(int cid);
};
class EPATracePagePool : public ProxyTracePagePool {
public:
virtual int command(int argc, const char*const* argv);
};
#endif //ns_pagepool_h
|