void afs_Daemon(void) { afs_int32 now, last10MinCheck, last60MinCheck; last10MinCheck = 0; last60MinCheck = 0; while (1) { rx_CheckPackets(); now = osi_Time(); if (last10MinCheck + 600 < now) { afs_GCUserData(); } if (last60MinCheck + 3600 < now) { afs_int32 didany; afs_GCPAGs(&didany); } now = 20000 - (osi_Time() - now); afs_osi_Wait(now, &AFS_WaitHandler, 0); if (afs_termState == AFSOP_STOP_AFS) { #if defined(RXK_LISTENER_ENV) afs_termState = AFSOP_STOP_RXEVENT; #else afs_termState = AFSOP_STOP_COMPLETE; #endif afs_osi_Wakeup(&afs_termState); return; } } }
/* This function always holds the GLOCK whilst it is running. The caller * gets the GLOCK before invoking it, and afs_osi_Sleep drops the GLOCK * whilst we are sleeping, and regains it when we're woken up. */ void afs_Daemon(void) { afs_int32 code; struct afs_exporter *exporter; afs_int32 now; afs_int32 last3MinCheck, last10MinCheck, last60MinCheck, lastNMinCheck; afs_int32 last1MinCheck, last5MinCheck; afs_uint32 lastCBSlotBump; char cs_warned = 0; AFS_STATCNT(afs_Daemon); afs_rootFid.Fid.Volume = 0; while (afs_initState < 101) afs_osi_Sleep(&afs_initState); #ifdef AFS_DARWIN80_ENV if (afs_osi_ctxtp_initialized) osi_Panic("vfs context already initialized"); while (afs_osi_ctxtp && vfs_context_ref) afs_osi_Sleep(&afs_osi_ctxtp); if (afs_osi_ctxtp && !vfs_context_ref) vfs_context_rele(afs_osi_ctxtp); afs_osi_ctxtp = vfs_context_create(NULL); afs_osi_ctxtp_initialized = 1; #endif now = osi_Time(); lastCBSlotBump = now; /* when a lot of clients are booted simultaneously, they develop * annoying synchronous VL server bashing behaviors. So we stagger them. */ last1MinCheck = now + ((afs_random() & 0x7fffffff) % 60); /* an extra 30 */ last3MinCheck = now - 90 + ((afs_random() & 0x7fffffff) % 180); last60MinCheck = now - 1800 + ((afs_random() & 0x7fffffff) % 3600); last10MinCheck = now - 300 + ((afs_random() & 0x7fffffff) % 600); last5MinCheck = now - 150 + ((afs_random() & 0x7fffffff) % 300); lastNMinCheck = now - 90 + ((afs_random() & 0x7fffffff) % 180); /* start off with afs_initState >= 101 (basic init done) */ while (1) { afs_CheckCallbacks(20); /* unstat anything which will expire soon */ /* things to do every 20 seconds or less - required by protocol spec */ if (afs_nfsexporter) afs_FlushActiveVcaches(0); /* flush NFS writes */ afs_FlushVCBs(1); /* flush queued callbacks */ afs_MaybeWakeupTruncateDaemon(); /* free cache space if have too */ rx_CheckPackets(); /* Does RX need more packets? */ now = osi_Time(); if (lastCBSlotBump + CBHTSLOTLEN < now) { /* pretty time-dependant */ lastCBSlotBump = now; if (afs_BumpBase()) { afs_CheckCallbacks(20); /* unstat anything which will expire soon */ } } if (last1MinCheck + 60 < now) { /* things to do every minute */ DFlush(); /* write out dir buffers */ afs_WriteThroughDSlots(); /* write through cacheinfo entries */ ObtainWriteLock(&afs_xvcache, 736); afs_FlushReclaimedVcaches(); ReleaseWriteLock(&afs_xvcache); afs_FlushActiveVcaches(1); /* keep flocks held & flush nfs writes */ #if 0 afs_StoreDirtyVcaches(); #endif afs_CheckRXEpoch(); last1MinCheck = now; } if (last3MinCheck + 180 < now) { afs_CheckTokenCache(); /* check for access cache resets due to expired * tickets */ last3MinCheck = now; } if (afsd_dynamic_vcaches && (last5MinCheck + 300 < now)) { /* start with trying to drop us back to our base usage */ int anumber = VCACHE_FREE + (afs_vcount - afs_cacheStats); if (anumber > 0) { ObtainWriteLock(&afs_xvcache, 734); afs_ShakeLooseVCaches(anumber); ReleaseWriteLock(&afs_xvcache); } last5MinCheck = now; } if (!afs_CheckServerDaemonStarted) { /* Do the check here if the correct afsd is not installed. */ if (!cs_warned) { cs_warned = 1; afs_warn("Please install afsd with check server daemon.\n"); } if (lastNMinCheck + afs_probe_interval < now) { /* only check down servers */ afs_CheckServers(1, NULL); lastNMinCheck = now; } } if (last10MinCheck + 600 < now) { #ifdef AFS_USERSPACE_IP_ADDR extern int rxi_GetcbiInfo(void); #endif afs_Trace1(afs_iclSetp, CM_TRACE_PROBEUP, ICL_TYPE_INT32, 600); #ifdef AFS_USERSPACE_IP_ADDR if (rxi_GetcbiInfo()) { /* addresses changed from last time */ afs_FlushCBs(); } #else /* AFS_USERSPACE_IP_ADDR */ if (rxi_GetIFInfo()) { /* addresses changed from last time */ afs_FlushCBs(); } #endif /* else AFS_USERSPACE_IP_ADDR */ if (!afs_CheckServerDaemonStarted) afs_CheckServers(0, NULL); afs_GCUserData(0); /* gc old conns */ /* This is probably the wrong way of doing GC for the various exporters but it will suffice for a while */ for (exporter = root_exported; exporter; exporter = exporter->exp_next) { (void)EXP_GC(exporter, 0); /* Generalize params */ } { static int cnt = 0; if (++cnt < 12) { afs_CheckVolumeNames(AFS_VOLCHECK_EXPIRED | AFS_VOLCHECK_BUSY); } else { cnt = 0; afs_CheckVolumeNames(AFS_VOLCHECK_EXPIRED | AFS_VOLCHECK_BUSY | AFS_VOLCHECK_MTPTS); } } last10MinCheck = now; } if (last60MinCheck + 3600 < now) { afs_Trace1(afs_iclSetp, CM_TRACE_PROBEVOLUME, ICL_TYPE_INT32, 3600); afs_CheckRootVolume(); #if AFS_GCPAGS if (afs_gcpags == AFS_GCPAGS_OK) { afs_int32 didany; afs_GCPAGs(&didany); } #endif last60MinCheck = now; } if (afs_initState < 300) { /* while things ain't rosy */ code = afs_CheckRootVolume(); if (code == 0) afs_initState = 300; /* succeeded */ if (afs_initState < 200) afs_initState = 200; /* tried once */ afs_osi_Wakeup(&afs_initState); } /* 18285 is because we're trying to divide evenly into 128, that is, * CBSlotLen, while staying just under 20 seconds. If CBSlotLen * changes, should probably change this interval, too. * Some of the preceding actions may take quite some time, so we * might not want to wait the entire interval */ now = 18285 - (osi_Time() - now); if (now > 0) { afs_osi_Wait(now, &AFS_WaitHandler, 0); } if (afs_termState == AFSOP_STOP_AFS) { if (afs_CheckServerDaemonStarted) afs_termState = AFSOP_STOP_CS; else afs_termState = AFSOP_STOP_TRUNCDAEMON; afs_osi_Wakeup(&afs_termState); return; } } }
static void rxi_ListenerProc(fd_set * rfds, int *tnop, struct rx_call **newcallp) { afs_uint32 host; u_short port; struct rx_packet *p = (struct rx_packet *)0; osi_socket socket; struct clock cv; afs_int32 nextPollTime; /* time to next poll FD before sleeping */ int lastPollWorked, doingPoll; /* true iff last poll was useful */ struct timeval tv, *tvp; int code; #ifdef AFS_NT40_ENV int i; #endif PROCESS pid; char name[MAXTHREADNAMELENGTH] = "srv_0"; clock_NewTime(); lastPollWorked = 0; nextPollTime = 0; code = LWP_CurrentProcess(&pid); if (code) { osi_Panic("rxi_Listener: Can't get my pid.\n"); } rx_listenerPid = pid; if (swapNameProgram) (*swapNameProgram) (pid, "listener", &name[0]); for (;;) { /* See if a check for additional packets was issued */ rx_CheckPackets(); /* Grab a new packet only if necessary (otherwise re-use the old one) */ if (p) { rxi_RestoreDataBufs(p); } else { if (!(p = rxi_AllocPacket(RX_PACKET_CLASS_RECEIVE))) osi_Panic("rxi_ListenerProc: no packets!"); /* Shouldn't happen */ } /* Wait for the next event time or a packet to arrive. */ /* event_RaiseEvents schedules any events whose time has come and * then atomically computes the time to the next event, guaranteeing * that this is positive. If there is no next event, it returns 0 */ clock_NewTime(); if (!rxevent_RaiseEvents(&cv)) tvp = NULL; else { /* It's important to copy cv to tv, because the 4.3 documentation * for select threatens that *tv may be updated after a select, in * future editions of the system, to indicate how much of the time * period has elapsed. So we shouldn't rely on tv not being altered. */ tv.tv_sec = cv.sec; /* Time to next event */ tv.tv_usec = cv.usec; tvp = &tv; } if (rx_stats_active) rx_atomic_inc(&rx_stats.selects); *rfds = rx_selectMask; if (lastPollWorked || nextPollTime < clock_Sec()) { /* we're catching up, or haven't tried to for a few seconds */ doingPoll = 1; nextPollTime = clock_Sec() + 4; /* try again in 4 seconds no matter what */ tv.tv_sec = tv.tv_usec = 0; /* make sure we poll */ tvp = &tv; code = select((int)(rx_maxSocketNumber + 1), rfds, 0, 0, tvp); } else { doingPoll = 0; code = IOMGR_Select((int)(rx_maxSocketNumber + 1), rfds, 0, 0, tvp); } lastPollWorked = 0; /* default is that it didn't find anything */ if (quitListening) { quitListening = 0; LWP_DestroyProcess(pid); } switch (code) { case 0: /* Timer interrupt: * If it was a timer interrupt then we can assume that * the time has advanced by roughly the value of the * previous timeout, and that there is now at least * one pending event. */ clock_NewTime(); break; case -1: /* select or IOMGR_Select returned failure */ debugSelectFailure++; /* update debugging counter */ clock_NewTime(); break; case -2: /* IOMGR_Cancel: * IOMGR_Cancel is invoked whenever a new event is * posted that is earlier than any existing events. * So we re-evaluate the time, and then go back to * reschedule events */ clock_NewTime(); break; default: /* Packets have arrived, presumably: * If it wasn't a timer interrupt, then no event should have * timed out yet (well some event may have, but only just...), so * we don't bother looking to see if any have timed out, but just * go directly to reading the data packets */ clock_NewTime(); if (doingPoll) lastPollWorked = 1; #ifdef AFS_NT40_ENV for (i = 0; p && i < rfds->fd_count; i++) { socket = rfds->fd_array[i]; if (rxi_ReadPacket(socket, p, &host, &port)) { *newcallp = NULL; p = rxi_ReceivePacket(p, socket, host, port, tnop, newcallp); if (newcallp && *newcallp) { if (p) { rxi_FreePacket(p); } if (swapNameProgram) { (*swapNameProgram) (rx_listenerPid, name, 0); rx_listenerPid = 0; } return; } } } #else for (socket = rx_minSocketNumber; p && socket <= rx_maxSocketNumber; socket++) { if (!FD_ISSET(socket, rfds)) continue; if (rxi_ReadPacket(socket, p, &host, &port)) { p = rxi_ReceivePacket(p, socket, host, port, tnop, newcallp); if (newcallp && *newcallp) { if (p) { rxi_FreePacket(p); } if (swapNameProgram) { (*swapNameProgram) (rx_listenerPid, name, 0); rx_listenerPid = 0; } return; } } } #endif break; } } /* NOTREACHED */ }