40 #define DEFAULT_WWW_DIR "www/"
41 #define DEFAULT_OCCUPANCY_RATE 0.2
42 #define DEFAULT_MAX_KB_SIZE UINT_MAX
43 #define DEFAULT_MAX_FILES UINT_MAX
44 #define CACHE_INIT_SYNTAX "<www_dir>, <max_cache_size_in_kb>, <max_cached_files>, " \
45 "<max_cache_occupancy_rate>"
47 #define MONITOR_CACHE_STATS
48 #define CACHE_HIT_STAT MSU_STAT1
49 #define CACHE_MISS_STAT MSU_STAT2
50 #define CACHE_EVICT_STAT MSU_STAT3
80 log_info(
"File %s retrieved from cache", path);
109 if (to_parse == NULL) {
110 log_warn(
"Initializing cache MSU with default parameters. "
114 if ( (tok = strtok_r(to_parse,
" ,", &saveptr)) == NULL) {
115 log_warn(
"Couldn't get wwW_dir from initialization string");
118 cache_state->
www_dir = malloc(32 + strlen(tok));
121 if ( (tok = strtok_r(NULL,
" ,", &saveptr)) == NULL) {
122 log_warn(
"Couldn't get max cache size in kb from initialization string");
125 int max_kb_size = atoi(tok);
126 if (max_kb_size >= 0)
127 cache_state->
max_kb_size = (
unsigned int) max_kb_size;
129 if ( (tok = strtok_r(NULL,
" ,", &saveptr)) == NULL) {
130 log_warn(
"Couldn't get max cached files from initialization string");
133 int max_files = atoi(tok);
135 cache_state->
max_files = (
unsigned int) max_files;
137 if ( (tok = strtok_r(NULL,
" ,", &saveptr)) == NULL) {
138 log_warn(
"Couldn't get max occupancy rate in kb from initialization string");
141 float max_occupancy_rate = atof(tok);
142 if (max_occupancy_rate >= 0.0)
145 if ( (tok = strtok_r(NULL,
" ,", &saveptr)) != NULL) {
146 log_warn(
"Discarding extra tokens from cache initialization: %s", tok);
154 float kbytes = (float) length / 1024;
156 log_info(
"File at %s is too large for caching (%ld bytes)", path, length);
164 if (cached == NULL) {
165 log_error(
"Trying to evict lru head that is NULL!");
170 #ifdef MONITOR_CACHE_STATS
192 if (cached == NULL) {
193 log_error(
"Failed to allocate space for cached_file struct for file %s", path);
197 log_info(
"Adding file %s to cache", path);
202 int path_len = strlen(path);
203 cached->
path = (
char *) malloc(path_len + 1);
204 strncpy(cached->
path, path, path_len);
205 cached->
path[path_len] =
'\0';
208 cached->
contents = (
char *) malloc(length);
209 memcpy(cached->
contents, contents, length);
243 #ifdef MONITOR_CACHE_STATS
250 char *mime_type = NULL;
262 #ifdef MONITOR_CACHE_STATS
277 struct ws_cache_state *cache_state = malloc(
sizeof(*cache_state));
283 cache_state->
cache = NULL;
287 self->msu_state = (
void*)cache_state;
289 cache_instance =
self;
291 #ifdef MONITOR_CACHE_STATS
301 .
name =
"Webserver_cache_msu",
#define HASH_ADD_STR(head, strfield, add)
Collecting statistics within the runtime.
struct cached_file * cache
int shortest_queue_route(struct msu_type *type, struct local_msu *sender, struct msu_msg *msg, struct msu_endpoint *output)
Chooses the local MSU with the shortest queue.
struct msu_type WEBSERVER_FILEIO_MSU_TYPE
#define log_info(fmt,...)
#define DEFAULT_OCCUPANCY_RATE
#define WEBSERVER_CACHE_MSU_TYPE_ID
Defines a type of MSU, including callback and accessor functions.
#define DEFAULT_MAX_KB_SIZE
#define CACHE_INIT_SYNTAX
struct msu_type WEBSERVER_CACHE_MSU_TYPE
Logging of status messages to the terminal.
Access local files within the repo.
char * path_to_mimetype(char *path)
unsigned int id
Unique ID for a local MSU.
struct msu_type WEBSERVER_WRITE_MSU_TYPE
struct cached_file * lru_head
struct cached_file * lru_next
Declares the methods available for calling an MSU from another MSU.
#define log_error(fmt,...)
Declares the structures and functions applicable to MSUs on the local machine.
int init_stat_item(enum stat_id stat_id, unsigned int item_id)
Initializes a stat item so that statistics can be logged to it.
static int ws_cache_init(struct local_msu *self, struct msu_init_data *init_data)
struct cached_file * lru_prev
#define HASH_FIND_STR(head, findstr, out)
The structure that represents an MSU located on the local machine.
struct cached_file * lru_tail
Data with which an MSU is initialized, and the payload for messages of type CTRL_CREATE_MSU.
static struct local_msu * cache_instance
static int cache_file(struct ws_cache_state *fc, char *path, char *contents, long length)
static int ws_cache_lookup(struct local_msu *self, struct msu_msg *msg)
int get_local_file(char *out, char *file)
Gets a file relative to the path of the executable.
int call_msu_type(struct local_msu *sender, struct msu_type *dst_type, struct msu_msg_hdr *hdr, size_t data_size, void *data)
Sends an MSU message to a destination of the given type, utilizing the sending MSU's routing function...
Interfaces for the creation and modification of the data-flow-graph and and general description of th...
static int parse_init_cache_payload(char *to_parse, struct ws_cache_state *cache_state)
int increment_stat(enum stat_id stat_id, unsigned int item_id, double value)
Increments the given statistic by the provided value.
int generate_header(char *dest, int code, int capacity, int body_len, char *mime_type)
A message that is to be delivered to an instance of an MSU.
struct cached_file * check_cache(struct ws_cache_state *fc, char *path)
char * name
Name for the msu type.
#define HASH_DEL(head, delptr)
int url_to_path(char *url, char *dir, char *path, int capacity)
#define log_warn(fmt,...)
#define DEFAULT_MAX_FILES
Declares strategies that MSUs can use for routing to endpoints.