static void mainInitialize(void) { //... //do nothing ... comm_select_postinit(); //configure_once代表是否曾经调用过mainInitialize()函数 //1表示之前调用过 if (!configured_once) //do nothing ... disk_init(); /* disk_init must go before ipcache_init() */ //域名和IP地址的缓存初始化,用来缓存dns正反解得到的ip //"ipcache_size"ip缓存的entry数量 //初始化IpcacheStats,一个统计数据的结构(如requests/replies/hits/misses等数据) //初始化lru_list,是一个双向链表 //初始化static_addrs,_ipcache_addrs结构 //设置ipcache_high和ipcache_low //创建一个哈希表ip_table,缓存ip //cachemgrRegister注册获取ipcache统计数据的action //分配内存池到MemPools的相应位置 ipcache_init() //fqdn缓存初始化 //"fqdncache_size"域名全称缓存的entry数量 //初始化类似ipcache_init(),初始化FqdncacheStats,创建一个哈希表fqdn_table,并注册获取统计数据的action,分配内存池到MemPools的相应位置 fqdncache_init(); //解析host_file指令指定的文件,一般linux下是"/etc/hosts",并把ip和host放入对应的ip_table和fqdn_table parseEtcHosts(); //使用外部(由cache_dns_program指定)或者内部的dns //调用helperOpenServers()启动外部程序,并建立进程间通信通道 //cachemgrRegister注册获取dns统计数据的action //... //同上,启动redirect_program指令指定的外部程序,注册获取redirect统计数据的action redirectInit(); //同上,storeurl_rewrite_program指令 storeurlInit(); //同上,location_rewrite_program指令 locationRewriteInit(); errorMapInit(); //"auth_param"指令 //代理认证初始化 authenticateInit(&Config.authConfig); //"external_acl_type"指令 //外部acl辅助器 externalAclInit(); //"external_refresh_check"指令 //外部辅助器,来决定是否refresh stale response refreshCheckInit(); //打开useragent_log指令指定的logfile useragentOpenLog(); //打开"referer_log"指令指定的logfile refererOpenLog(); //初始化http header中各个域的相关数据结构 //设置ListHeadersMask,ReplyHeadersMask,RequestHeadersMask的位 //初始化HttpHeaderStats,并注册一个获取HttpHeaderStats信息的action //must go before any header processing (e.g. the one in errorInitialize) httpHeaderInitModule(); //This function finds the error messages formats, and stores them in error_text[]; errorInitialize(); //打开"access_log","cache_access_log"指令指定的logfile accessLogInit(); //... //第一次调用mainInitialize() if(!configured_once) { //调用cachemgrRegister()注册回调函数,如cachemgrShutdown,cachemgrReconfigure //cachemgrRegister()把action及它的回调函数加入到ActionTable里面 cachemgrInit(); //统计数据初始化 //注册获取各种统计数据的action statInit(); //store_table哈希列表,cache摘要,swap dir等初始化, storeInit(); //读取mine文件,把里面的类型存入mime table mimeInit(Config.mimeTablePathname); //持久化连接table初始化 //client_pconn_hist,server_pconn_hist初始化 //给pconn_data_pool,pconn_fds_pool分配内存 pconnInit(); //初始化refreshCounts中每个协议(HTTP, ICP, HTCP, on store, Cache Digests) //初始化DefaultRefresh (_refresh_t结构) refreshInit(); //调用clientOpenListenSockets()创建并监听socket,socket的地址由"http_port"指定 //把socket的fd注册到事件监听(如epoll),并注册请求处理函数 //调用icpConnectionsOpen()打开ICP socket(incoming和outgoing, 都是udp),并添加到事件监听和处理函数 serverConnectionsOpen(); //ICP neighbors_init(); //添加一些信号的处理函数 //... //检查MemPools[]的所有entry是否已经初始化 //... //添加事件 //... configured_once = 1; } }
void storeInit(void) { //初始化一个大小为16个unsigned char的数组null_key storeKeyInit(); //计算store bucket的个数store_hash_buckets storeInitHashValues(); //创建store_table,bucket个数是store_hash_buckets store_table = hash_create(storeKeyHashCmp, store_hash_buckets, storeKeyHashHash); //Create removal policy instance mem_policy = createRemovalPolicy(Config.memPolicy); //Cache摘要(Cache Digest) //Cache摘要基于由Pei Cao首先发布的一项技术,叫做摘要缓存。基本思路是用一个Bloom filter来表现cache内容。邻居cache下载其他每个cache的Bloom filter(也即摘要)。然后,通过查询摘要来决定某个URI是否在邻居的cache里。 //相对于ICP,cache摘要以空间交换时间。ICP查询浪费时间(延时),cache摘要浪费空间(内存,磁盘)。在squid中,邻居的摘要完全存放在内存里。在一个典型的摘要里,每百万目标需要大约625KB的内存。 //digest_generation on #开启.本参数指令控制squid是否产生自己的cache摘要 //创建cache摘要store_digest并注册获取cache摘要的action storeDigestInit(); //打开cache_store_log指定的logfile storeLogOpen(); stackInit(&LateReleaseStack); //添加一个event到tasks队列 eventAdd("storeLateRelease", storeLateRelease, NULL, 1.0, 1); //调用SwapDir结构的init()函数来初始化swap fs的bitmap。如aufs就是storeAufsDirInit() storeDirInit(); storeRebuildStart(); //注册actions cachemgrRegister("storedir", "Store Directory Stats", storeDirStats, 0, 1); cachemgrRegister("store_check_cachable_stats", "storeCheckCachable() Stats", storeCheckCachableStats, 0, 1); cachemgrRegister("store_io", "Store IO Interface Stats", storeIOStats, 0, 1); }