ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev.c
(Generate patch)

Comparing libev/ev.c (file contents):
Revision 1.231 by root, Mon May 5 20:47:33 2008 UTC vs.
Revision 1.235 by root, Wed May 7 14:45:17 2008 UTC

520 } 520 }
521} 521}
522 522
523/*****************************************************************************/ 523/*****************************************************************************/
524 524
525#define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */
526
525int inline_size 527int inline_size
526array_nextsize (int elem, int cur, int cnt) 528array_nextsize (int elem, int cur, int cnt)
527{ 529{
528 int ncur = cur + 1; 530 int ncur = cur + 1;
529 531
530 do 532 do
531 ncur <<= 1; 533 ncur <<= 1;
532 while (cnt > ncur); 534 while (cnt > ncur);
533 535
534 /* if size > 4096, round to 4096 - 4 * longs to accomodate malloc overhead */ 536 /* if size is large, round to MALLOC_ROUND - 4 * longs to accomodate malloc overhead */
535 if (elem * ncur > 4096) 537 if (elem * ncur > MALLOC_ROUND - sizeof (void *) * 4)
536 { 538 {
537 ncur *= elem; 539 ncur *= elem;
538 ncur = (ncur + elem + 4095 + sizeof (void *) * 4) & ~4095; 540 ncur = (ncur + elem + (MALLOC_ROUND - 1) + sizeof (void *) * 4) & ~(MALLOC_ROUND - 1);
539 ncur = ncur - sizeof (void *) * 4; 541 ncur = ncur - sizeof (void *) * 4;
540 ncur /= elem; 542 ncur /= elem;
541 } 543 }
542 544
543 return ncur; 545 return ncur;
757 } 759 }
758} 760}
759 761
760/*****************************************************************************/ 762/*****************************************************************************/
761 763
764/*
765 * at the moment we allow libev the luxury of two heaps,
766 * a small-code-size 2-heap one and a ~1.5kb larger 4-heap
767 * which is more cache-efficient.
768 * the difference is about 5% with 50000+ watchers.
769 */
770#define USE_4HEAP !EV_MINIMAL
771#if USE_4HEAP
772
773#define HEAP0 3 /* index of first element in heap */
774
762/* towards the root */ 775/* towards the root */
763void inline_speed 776void inline_speed
764upheap (WT *heap, int k) 777upheap (WT *heap, int k)
765{ 778{
766 WT w = heap [k]; 779 WT w = heap [k];
767 780
768 for (;;) 781 for (;;)
769 { 782 {
783 int p = ((k - HEAP0 - 1) / 4) + HEAP0;
784
785 if (p >= HEAP0 || heap [p]->at <= w->at)
786 break;
787
788 heap [k] = heap [p];
789 ev_active (heap [k]) = k;
790 k = p;
791 }
792
793 heap [k] = w;
794 ev_active (heap [k]) = k;
795}
796
797/* away from the root */
798void inline_speed
799downheap (WT *heap, int N, int k)
800{
801 WT w = heap [k];
802 WT *E = heap + N + HEAP0;
803
804 for (;;)
805 {
806 ev_tstamp minat;
807 WT *minpos;
808 WT *pos = heap + 4 * (k - HEAP0) + HEAP0;
809
810 // find minimum child
811 if (expect_true (pos +3 < E))
812 {
813 (minpos = pos + 0), (minat = (*minpos)->at);
814 if (pos [1]->at < minat) (minpos = pos + 1), (minat = (*minpos)->at);
815 if (pos [2]->at < minat) (minpos = pos + 2), (minat = (*minpos)->at);
816 if (pos [3]->at < minat) (minpos = pos + 3), (minat = (*minpos)->at);
817 }
818 else
819 {
820 if (pos >= E)
821 break;
822
823 (minpos = pos + 0), (minat = (*minpos)->at);
824 if (pos + 1 < E && pos [1]->at < minat) (minpos = pos + 1), (minat = (*minpos)->at);
825 if (pos + 2 < E && pos [2]->at < minat) (minpos = pos + 2), (minat = (*minpos)->at);
826 if (pos + 3 < E && pos [3]->at < minat) (minpos = pos + 3), (minat = (*minpos)->at);
827 }
828
829 if (w->at <= minat)
830 break;
831
832 ev_active (*minpos) = k;
833 heap [k] = *minpos;
834
835 k = minpos - heap;
836 }
837
838 heap [k] = w;
839 ev_active (heap [k]) = k;
840}
841
842#else // 4HEAP
843
844#define HEAP0 1
845
846/* towards the root */
847void inline_speed
848upheap (WT *heap, int k)
849{
850 WT w = heap [k];
851
852 for (;;)
853 {
770 int p = k >> 1; 854 int p = k >> 1;
771 855
772 /* maybe we could use a dummy element at heap [0]? */ 856 /* maybe we could use a dummy element at heap [0]? */
773 if (!p || heap [p]->at <= w->at) 857 if (!p || heap [p]->at <= w->at)
774 break; 858 break;
793 int c = k << 1; 877 int c = k << 1;
794 878
795 if (c > N) 879 if (c > N)
796 break; 880 break;
797 881
798 c += c < N && heap [c]->at > heap [c + 1]->at 882 c += c + 1 < N && heap [c]->at > heap [c + 1]->at
799 ? 1 : 0; 883 ? 1 : 0;
800 884
801 if (w->at <= heap [c]->at) 885 if (w->at <= heap [c]->at)
802 break; 886 break;
803 887
804 heap [k] = heap [c]; 888 heap [k] = heap [c];
805 ev_active (heap [k]) = k; 889 ((W)heap [k])->active = k;
806 890
807 k = c; 891 k = c;
808 } 892 }
809 893
810 heap [k] = w; 894 heap [k] = w;
811 ev_active (heap [k]) = k; 895 ev_active (heap [k]) = k;
812} 896}
897#endif
813 898
814void inline_size 899void inline_size
815adjustheap (WT *heap, int N, int k) 900adjustheap (WT *heap, int N, int k)
816{ 901{
817 upheap (heap, k); 902 upheap (heap, k);
912pipecb (EV_P_ ev_io *iow, int revents) 997pipecb (EV_P_ ev_io *iow, int revents)
913{ 998{
914#if EV_USE_EVENTFD 999#if EV_USE_EVENTFD
915 if (evfd >= 0) 1000 if (evfd >= 0)
916 { 1001 {
917 uint64_t counter = 1; 1002 uint64_t counter;
918 read (evfd, &counter, sizeof (uint64_t)); 1003 read (evfd, &counter, sizeof (uint64_t));
919 } 1004 }
920 else 1005 else
921#endif 1006#endif
922 { 1007 {
1368void 1453void
1369ev_loop_fork (EV_P) 1454ev_loop_fork (EV_P)
1370{ 1455{
1371 postfork = 1; /* must be in line with ev_default_fork */ 1456 postfork = 1; /* must be in line with ev_default_fork */
1372} 1457}
1373
1374#endif 1458#endif
1375 1459
1376#if EV_MULTIPLICITY 1460#if EV_MULTIPLICITY
1377struct ev_loop * 1461struct ev_loop *
1378ev_default_loop_init (unsigned int flags) 1462ev_default_loop_init (unsigned int flags)
1459 EV_CB_INVOKE (p->w, p->events); 1543 EV_CB_INVOKE (p->w, p->events);
1460 } 1544 }
1461 } 1545 }
1462} 1546}
1463 1547
1548#if EV_IDLE_ENABLE
1549void inline_size
1550idle_reify (EV_P)
1551{
1552 if (expect_false (idleall))
1553 {
1554 int pri;
1555
1556 for (pri = NUMPRI; pri--; )
1557 {
1558 if (pendingcnt [pri])
1559 break;
1560
1561 if (idlecnt [pri])
1562 {
1563 queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE);
1564 break;
1565 }
1566 }
1567 }
1568}
1569#endif
1570
1464void inline_size 1571void inline_size
1465timers_reify (EV_P) 1572timers_reify (EV_P)
1466{ 1573{
1467 while (timercnt && ev_at (timers [1]) <= mn_now) 1574 while (timercnt && ev_at (timers [HEAP0]) <= mn_now)
1468 { 1575 {
1469 ev_timer *w = (ev_timer *)timers [1]; 1576 ev_timer *w = (ev_timer *)timers [HEAP0];
1470 1577
1471 /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/ 1578 /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/
1472 1579
1473 /* first reschedule or stop timer */ 1580 /* first reschedule or stop timer */
1474 if (w->repeat) 1581 if (w->repeat)
1477 1584
1478 ev_at (w) += w->repeat; 1585 ev_at (w) += w->repeat;
1479 if (ev_at (w) < mn_now) 1586 if (ev_at (w) < mn_now)
1480 ev_at (w) = mn_now; 1587 ev_at (w) = mn_now;
1481 1588
1482 downheap (timers, timercnt, 1); 1589 downheap (timers, timercnt, HEAP0);
1483 } 1590 }
1484 else 1591 else
1485 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 1592 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
1486 1593
1487 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); 1594 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
1490 1597
1491#if EV_PERIODIC_ENABLE 1598#if EV_PERIODIC_ENABLE
1492void inline_size 1599void inline_size
1493periodics_reify (EV_P) 1600periodics_reify (EV_P)
1494{ 1601{
1495 while (periodiccnt && ev_at (periodics [1]) <= ev_rt_now) 1602 while (periodiccnt && ev_at (periodics [HEAP0]) <= ev_rt_now)
1496 { 1603 {
1497 ev_periodic *w = (ev_periodic *)periodics [1]; 1604 ev_periodic *w = (ev_periodic *)periodics [HEAP0];
1498 1605
1499 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/ 1606 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/
1500 1607
1501 /* first reschedule or stop timer */ 1608 /* first reschedule or stop timer */
1502 if (w->reschedule_cb) 1609 if (w->reschedule_cb)
1508 else if (w->interval) 1615 else if (w->interval)
1509 { 1616 {
1510 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; 1617 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1511 if (ev_at (w) - ev_rt_now <= TIME_EPSILON) ev_at (w) += w->interval; 1618 if (ev_at (w) - ev_rt_now <= TIME_EPSILON) ev_at (w) += w->interval;
1512 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ev_at (w) > ev_rt_now)); 1619 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ev_at (w) > ev_rt_now));
1513 downheap (periodics, periodiccnt, 1); 1620 downheap (periodics, periodiccnt, HEAP0);
1514 } 1621 }
1515 else 1622 else
1516 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 1623 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1517 1624
1518 ev_feed_event (EV_A_ (W)w, EV_PERIODIC); 1625 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
1534 else if (w->interval) 1641 else if (w->interval)
1535 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; 1642 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1536 } 1643 }
1537 1644
1538 /* now rebuild the heap */ 1645 /* now rebuild the heap */
1539 for (i = periodiccnt >> 1; i--; ) 1646 for (i = periodiccnt >> 1; --i; )
1540 downheap (periodics, periodiccnt, i); 1647 downheap (periodics, periodiccnt, i + HEAP0);
1541}
1542#endif
1543
1544#if EV_IDLE_ENABLE
1545void inline_size
1546idle_reify (EV_P)
1547{
1548 if (expect_false (idleall))
1549 {
1550 int pri;
1551
1552 for (pri = NUMPRI; pri--; )
1553 {
1554 if (pendingcnt [pri])
1555 break;
1556
1557 if (idlecnt [pri])
1558 {
1559 queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE);
1560 break;
1561 }
1562 }
1563 }
1564} 1648}
1565#endif 1649#endif
1566 1650
1567void inline_speed 1651void inline_speed
1568time_update (EV_P_ ev_tstamp max_block) 1652time_update (EV_P_ ev_tstamp max_block)
1597 */ 1681 */
1598 for (i = 4; --i; ) 1682 for (i = 4; --i; )
1599 { 1683 {
1600 rtmn_diff = ev_rt_now - mn_now; 1684 rtmn_diff = ev_rt_now - mn_now;
1601 1685
1602 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) 1686 if (expect_true (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP))
1603 return; /* all is well */ 1687 return; /* all is well */
1604 1688
1605 ev_rt_now = ev_time (); 1689 ev_rt_now = ev_time ();
1606 mn_now = get_clock (); 1690 mn_now = get_clock ();
1607 now_floor = mn_now; 1691 now_floor = mn_now;
1703 1787
1704 waittime = MAX_BLOCKTIME; 1788 waittime = MAX_BLOCKTIME;
1705 1789
1706 if (timercnt) 1790 if (timercnt)
1707 { 1791 {
1708 ev_tstamp to = ev_at (timers [1]) - mn_now + backend_fudge; 1792 ev_tstamp to = ev_at (timers [HEAP0]) - mn_now + backend_fudge;
1709 if (waittime > to) waittime = to; 1793 if (waittime > to) waittime = to;
1710 } 1794 }
1711 1795
1712#if EV_PERIODIC_ENABLE 1796#if EV_PERIODIC_ENABLE
1713 if (periodiccnt) 1797 if (periodiccnt)
1714 { 1798 {
1715 ev_tstamp to = ev_at (periodics [1]) - ev_rt_now + backend_fudge; 1799 ev_tstamp to = ev_at (periodics [HEAP0]) - ev_rt_now + backend_fudge;
1716 if (waittime > to) waittime = to; 1800 if (waittime > to) waittime = to;
1717 } 1801 }
1718#endif 1802#endif
1719 1803
1720 if (expect_false (waittime < timeout_blocktime)) 1804 if (expect_false (waittime < timeout_blocktime))
1890 1974
1891 ev_at (w) += mn_now; 1975 ev_at (w) += mn_now;
1892 1976
1893 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1977 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1894 1978
1895 ev_start (EV_A_ (W)w, ++timercnt); 1979 ev_start (EV_A_ (W)w, ++timercnt + HEAP0 - 1);
1896 array_needsize (WT, timers, timermax, timercnt + 1, EMPTY2); 1980 array_needsize (WT, timers, timermax, timercnt + HEAP0, EMPTY2);
1897 timers [timercnt] = (WT)w; 1981 timers [ev_active (w)] = (WT)w;
1898 upheap (timers, timercnt); 1982 upheap (timers, ev_active (w));
1899 1983
1900 /*assert (("internal timer heap corruption", timers [ev_active (w)] == w));*/ 1984 /*assert (("internal timer heap corruption", timers [ev_active (w)] == w));*/
1901} 1985}
1902 1986
1903void noinline 1987void noinline
1910 { 1994 {
1911 int active = ev_active (w); 1995 int active = ev_active (w);
1912 1996
1913 assert (("internal timer heap corruption", timers [active] == (WT)w)); 1997 assert (("internal timer heap corruption", timers [active] == (WT)w));
1914 1998
1915 if (expect_true (active < timercnt)) 1999 if (expect_true (active < timercnt + HEAP0 - 1))
1916 { 2000 {
1917 timers [active] = timers [timercnt]; 2001 timers [active] = timers [timercnt + HEAP0 - 1];
1918 adjustheap (timers, timercnt, active); 2002 adjustheap (timers, timercnt, active);
1919 } 2003 }
1920 2004
1921 --timercnt; 2005 --timercnt;
1922 } 2006 }
1962 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; 2046 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1963 } 2047 }
1964 else 2048 else
1965 ev_at (w) = w->offset; 2049 ev_at (w) = w->offset;
1966 2050
1967 ev_start (EV_A_ (W)w, ++periodiccnt); 2051 ev_start (EV_A_ (W)w, ++periodiccnt + HEAP0 - 1);
1968 array_needsize (WT, periodics, periodicmax, periodiccnt + 1, EMPTY2); 2052 array_needsize (WT, periodics, periodicmax, periodiccnt + HEAP0, EMPTY2);
1969 periodics [periodiccnt] = (WT)w; 2053 periodics [ev_active (w)] = (WT)w;
1970 upheap (periodics, periodiccnt); 2054 upheap (periodics, ev_active (w));
1971 2055
1972 /*assert (("internal periodic heap corruption", periodics [ev_active (w)] == w));*/ 2056 /*assert (("internal periodic heap corruption", periodics [ev_active (w)] == w));*/
1973} 2057}
1974 2058
1975void noinline 2059void noinline
1982 { 2066 {
1983 int active = ev_active (w); 2067 int active = ev_active (w);
1984 2068
1985 assert (("internal periodic heap corruption", periodics [active] == (WT)w)); 2069 assert (("internal periodic heap corruption", periodics [active] == (WT)w));
1986 2070
1987 if (expect_true (active < periodiccnt)) 2071 if (expect_true (active < periodiccnt + HEAP0 - 1))
1988 { 2072 {
1989 periodics [active] = periodics [periodiccnt]; 2073 periodics [active] = periodics [periodiccnt + HEAP0 - 1];
1990 adjustheap (periodics, periodiccnt, active); 2074 adjustheap (periodics, periodiccnt, active);
1991 } 2075 }
1992 2076
1993 --periodiccnt; 2077 --periodiccnt;
1994 } 2078 }
2114 if (w->wd < 0) 2198 if (w->wd < 0)
2115 { 2199 {
2116 ev_timer_start (EV_A_ &w->timer); /* this is not race-free, so we still need to recheck periodically */ 2200 ev_timer_start (EV_A_ &w->timer); /* this is not race-free, so we still need to recheck periodically */
2117 2201
2118 /* monitor some parent directory for speedup hints */ 2202 /* monitor some parent directory for speedup hints */
2203 /* note that exceeding the hardcoded limit is not a correctness issue, */
2204 /* but an efficiency issue only */
2119 if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096) 2205 if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096)
2120 { 2206 {
2121 char path [4096]; 2207 char path [4096];
2122 strcpy (path, w->path); 2208 strcpy (path, w->path);
2123 2209

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines