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

Comparing libev/ev.c (file contents):
Revision 1.230 by root, Fri May 2 08:13:16 2008 UTC vs.
Revision 1.238 by root, Thu May 8 20:49:12 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#define USE_4HEAP 1/* they do not work corretcly */
772#if USE_4HEAP
773
774#define DHEAP 4
775#define HEAP0 (DHEAP - 1) /* index of first element in heap */
776
762/* towards the root */ 777/* towards the root */
763void inline_speed 778void inline_speed
764upheap (WT *heap, int k) 779upheap (WT *heap, int k)
765{ 780{
766 WT w = heap [k]; 781 WT w = heap [k];
767 782
768 for (;;) 783 for (;;)
769 { 784 {
785 int p = ((k - HEAP0 - 1) / DHEAP) + HEAP0;
786
787 if (p == k || heap [p]->at <= w->at)
788 break;
789
790 heap [k] = heap [p];
791 ev_active (heap [k]) = k;
792 k = p;
793 }
794
795 heap [k] = w;
796 ev_active (heap [k]) = k;
797}
798
799/* away from the root */
800void inline_speed
801downheap (WT *heap, int N, int k)
802{
803 WT w = heap [k];
804 WT *E = heap + N + HEAP0;
805
806 for (;;)
807 {
808 ev_tstamp minat;
809 WT *minpos;
810 WT *pos = heap + DHEAP * (k - HEAP0) + HEAP0;
811
812 // find minimum child
813 if (expect_true (pos + DHEAP - 1 < E))
814 {
815 /* fast path */
816 (minpos = pos + 0), (minat = (*minpos)->at);
817 if (pos [1]->at < minat) (minpos = pos + 1), (minat = (*minpos)->at);
818 if (pos [2]->at < minat) (minpos = pos + 2), (minat = (*minpos)->at);
819 if (pos [3]->at < minat) (minpos = pos + 3), (minat = (*minpos)->at);
820 }
821 else
822 {
823 /* slow path */
824 if (pos >= E)
825 break;
826 (minpos = pos + 0), (minat = (*minpos)->at);
827 if (pos + 1 < E && pos [1]->at < minat) (minpos = pos + 1), (minat = (*minpos)->at);
828 if (pos + 2 < E && pos [2]->at < minat) (minpos = pos + 2), (minat = (*minpos)->at);
829 if (pos + 3 < E && pos [3]->at < minat) (minpos = pos + 3), (minat = (*minpos)->at);
830 }
831
832 if (w->at <= minat)
833 break;
834
835 ev_active (*minpos) = k;
836 heap [k] = *minpos;
837
838 k = minpos - heap;
839 }
840
841 heap [k] = w;
842 ev_active (heap [k]) = k;
843}
844
845#else // 4HEAP
846
847#define HEAP0 1
848
849/* towards the root */
850void inline_speed
851upheap (WT *heap, int k)
852{
853 WT w = heap [k];
854
855 for (;;)
856 {
770 int p = k >> 1; 857 int p = k >> 1;
771 858
772 /* maybe we could use a dummy element at heap [0]? */ 859 /* maybe we could use a dummy element at heap [0]? */
773 if (!p || heap [p]->at <= w->at) 860 if (!p || heap [p]->at <= w->at)
774 break; 861 break;
793 int c = k << 1; 880 int c = k << 1;
794 881
795 if (c > N) 882 if (c > N)
796 break; 883 break;
797 884
798 c += c < N && heap [c]->at > heap [c + 1]->at 885 c += c + 1 < N && heap [c]->at > heap [c + 1]->at
799 ? 1 : 0; 886 ? 1 : 0;
800 887
801 if (w->at <= heap [c]->at) 888 if (w->at <= heap [c]->at)
802 break; 889 break;
803 890
804 heap [k] = heap [c]; 891 heap [k] = heap [c];
805 ev_active (heap [k]) = k; 892 ((W)heap [k])->active = k;
806 893
807 k = c; 894 k = c;
808 } 895 }
809 896
810 heap [k] = w; 897 heap [k] = w;
811 ev_active (heap [k]) = k; 898 ev_active (heap [k]) = k;
812} 899}
900#endif
813 901
814void inline_size 902void inline_size
815adjustheap (WT *heap, int N, int k) 903adjustheap (WT *heap, int N, int k)
816{ 904{
817 upheap (heap, k); 905 upheap (heap, k);
912pipecb (EV_P_ ev_io *iow, int revents) 1000pipecb (EV_P_ ev_io *iow, int revents)
913{ 1001{
914#if EV_USE_EVENTFD 1002#if EV_USE_EVENTFD
915 if (evfd >= 0) 1003 if (evfd >= 0)
916 { 1004 {
917 uint64_t counter = 1; 1005 uint64_t counter;
918 read (evfd, &counter, sizeof (uint64_t)); 1006 read (evfd, &counter, sizeof (uint64_t));
919 } 1007 }
920 else 1008 else
921#endif 1009#endif
922 { 1010 {
1368void 1456void
1369ev_loop_fork (EV_P) 1457ev_loop_fork (EV_P)
1370{ 1458{
1371 postfork = 1; /* must be in line with ev_default_fork */ 1459 postfork = 1; /* must be in line with ev_default_fork */
1372} 1460}
1373
1374#endif 1461#endif
1375 1462
1376#if EV_MULTIPLICITY 1463#if EV_MULTIPLICITY
1377struct ev_loop * 1464struct ev_loop *
1378ev_default_loop_init (unsigned int flags) 1465ev_default_loop_init (unsigned int flags)
1459 EV_CB_INVOKE (p->w, p->events); 1546 EV_CB_INVOKE (p->w, p->events);
1460 } 1547 }
1461 } 1548 }
1462} 1549}
1463 1550
1551#if EV_IDLE_ENABLE
1552void inline_size
1553idle_reify (EV_P)
1554{
1555 if (expect_false (idleall))
1556 {
1557 int pri;
1558
1559 for (pri = NUMPRI; pri--; )
1560 {
1561 if (pendingcnt [pri])
1562 break;
1563
1564 if (idlecnt [pri])
1565 {
1566 queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE);
1567 break;
1568 }
1569 }
1570 }
1571}
1572#endif
1573
1464void inline_size 1574void inline_size
1465timers_reify (EV_P) 1575timers_reify (EV_P)
1466{ 1576{
1467 while (timercnt && ev_at (timers [1]) <= mn_now) 1577 while (timercnt && ev_at (timers [HEAP0]) <= mn_now)
1468 { 1578 {
1469 ev_timer *w = (ev_timer *)timers [1]; 1579 ev_timer *w = (ev_timer *)timers [HEAP0];
1470 1580
1471 /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/ 1581 /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/
1472 1582
1473 /* first reschedule or stop timer */ 1583 /* first reschedule or stop timer */
1474 if (w->repeat) 1584 if (w->repeat)
1477 1587
1478 ev_at (w) += w->repeat; 1588 ev_at (w) += w->repeat;
1479 if (ev_at (w) < mn_now) 1589 if (ev_at (w) < mn_now)
1480 ev_at (w) = mn_now; 1590 ev_at (w) = mn_now;
1481 1591
1482 downheap (timers, timercnt, 1); 1592 downheap (timers, timercnt, HEAP0);
1483 } 1593 }
1484 else 1594 else
1485 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 1595 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
1486 1596
1487 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); 1597 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
1490 1600
1491#if EV_PERIODIC_ENABLE 1601#if EV_PERIODIC_ENABLE
1492void inline_size 1602void inline_size
1493periodics_reify (EV_P) 1603periodics_reify (EV_P)
1494{ 1604{
1495 while (periodiccnt && ev_at (periodics [1]) <= ev_rt_now) 1605 while (periodiccnt && ev_at (periodics [HEAP0]) <= ev_rt_now)
1496 { 1606 {
1497 ev_periodic *w = (ev_periodic *)periodics [1]; 1607 ev_periodic *w = (ev_periodic *)periodics [HEAP0];
1498 1608
1499 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/ 1609 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/
1500 1610
1501 /* first reschedule or stop timer */ 1611 /* first reschedule or stop timer */
1502 if (w->reschedule_cb) 1612 if (w->reschedule_cb)
1508 else if (w->interval) 1618 else if (w->interval)
1509 { 1619 {
1510 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; 1620 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; 1621 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)); 1622 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ev_at (w) > ev_rt_now));
1513 downheap (periodics, periodiccnt, 1); 1623 downheap (periodics, periodiccnt, HEAP0);
1514 } 1624 }
1515 else 1625 else
1516 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 1626 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1517 1627
1518 ev_feed_event (EV_A_ (W)w, EV_PERIODIC); 1628 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
1523periodics_reschedule (EV_P) 1633periodics_reschedule (EV_P)
1524{ 1634{
1525 int i; 1635 int i;
1526 1636
1527 /* adjust periodics after time jump */ 1637 /* adjust periodics after time jump */
1528 for (i = 0; i < periodiccnt; ++i) 1638 for (i = 1; i <= periodiccnt; ++i)
1529 { 1639 {
1530 ev_periodic *w = (ev_periodic *)periodics [i]; 1640 ev_periodic *w = (ev_periodic *)periodics [i];
1531 1641
1532 if (w->reschedule_cb) 1642 if (w->reschedule_cb)
1533 ev_at (w) = w->reschedule_cb (w, ev_rt_now); 1643 ev_at (w) = w->reschedule_cb (w, ev_rt_now);
1534 else if (w->interval) 1644 else if (w->interval)
1535 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; 1645 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1536 } 1646 }
1537 1647
1538 /* now rebuild the heap */ 1648 /* now rebuild the heap */
1539 for (i = periodiccnt >> 1; i--; ) 1649 for (i = periodiccnt >> 1; --i; )
1540 downheap (periodics, periodiccnt, i); 1650 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} 1651}
1565#endif 1652#endif
1566 1653
1567void inline_speed 1654void inline_speed
1568time_update (EV_P_ ev_tstamp max_block) 1655time_update (EV_P_ ev_tstamp max_block)
1597 */ 1684 */
1598 for (i = 4; --i; ) 1685 for (i = 4; --i; )
1599 { 1686 {
1600 rtmn_diff = ev_rt_now - mn_now; 1687 rtmn_diff = ev_rt_now - mn_now;
1601 1688
1602 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) 1689 if (expect_true (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP))
1603 return; /* all is well */ 1690 return; /* all is well */
1604 1691
1605 ev_rt_now = ev_time (); 1692 ev_rt_now = ev_time ();
1606 mn_now = get_clock (); 1693 mn_now = get_clock ();
1607 now_floor = mn_now; 1694 now_floor = mn_now;
1703 1790
1704 waittime = MAX_BLOCKTIME; 1791 waittime = MAX_BLOCKTIME;
1705 1792
1706 if (timercnt) 1793 if (timercnt)
1707 { 1794 {
1708 ev_tstamp to = ev_at (timers [1]) - mn_now + backend_fudge; 1795 ev_tstamp to = ev_at (timers [HEAP0]) - mn_now + backend_fudge;
1709 if (waittime > to) waittime = to; 1796 if (waittime > to) waittime = to;
1710 } 1797 }
1711 1798
1712#if EV_PERIODIC_ENABLE 1799#if EV_PERIODIC_ENABLE
1713 if (periodiccnt) 1800 if (periodiccnt)
1714 { 1801 {
1715 ev_tstamp to = ev_at (periodics [1]) - ev_rt_now + backend_fudge; 1802 ev_tstamp to = ev_at (periodics [HEAP0]) - ev_rt_now + backend_fudge;
1716 if (waittime > to) waittime = to; 1803 if (waittime > to) waittime = to;
1717 } 1804 }
1718#endif 1805#endif
1719 1806
1720 if (expect_false (waittime < timeout_blocktime)) 1807 if (expect_false (waittime < timeout_blocktime))
1890 1977
1891 ev_at (w) += mn_now; 1978 ev_at (w) += mn_now;
1892 1979
1893 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1980 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1894 1981
1895 ev_start (EV_A_ (W)w, ++timercnt); 1982 ev_start (EV_A_ (W)w, ++timercnt + HEAP0 - 1);
1896 array_needsize (WT, timers, timermax, timercnt + 1, EMPTY2); 1983 array_needsize (WT, timers, timermax, timercnt + HEAP0, EMPTY2);
1897 timers [timercnt] = (WT)w; 1984 timers [ev_active (w)] = (WT)w;
1898 upheap (timers, timercnt); 1985 upheap (timers, ev_active (w));
1899 1986
1900 /*assert (("internal timer heap corruption", timers [ev_active (w)] == w));*/ 1987 /*assert (("internal timer heap corruption", timers [ev_active (w)] == w));*/
1901} 1988}
1902 1989
1903void noinline 1990void noinline
1910 { 1997 {
1911 int active = ev_active (w); 1998 int active = ev_active (w);
1912 1999
1913 assert (("internal timer heap corruption", timers [active] == (WT)w)); 2000 assert (("internal timer heap corruption", timers [active] == (WT)w));
1914 2001
1915 if (expect_true (active < timercnt)) 2002 if (expect_true (active < timercnt + HEAP0 - 1))
1916 { 2003 {
1917 timers [active] = timers [timercnt]; 2004 timers [active] = timers [timercnt + HEAP0 - 1];
1918 adjustheap (timers, timercnt, active); 2005 adjustheap (timers, timercnt, active);
1919 } 2006 }
1920 2007
1921 --timercnt; 2008 --timercnt;
1922 } 2009 }
1962 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; 2049 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1963 } 2050 }
1964 else 2051 else
1965 ev_at (w) = w->offset; 2052 ev_at (w) = w->offset;
1966 2053
1967 ev_start (EV_A_ (W)w, ++periodiccnt); 2054 ev_start (EV_A_ (W)w, ++periodiccnt + HEAP0 - 1);
1968 array_needsize (WT, periodics, periodicmax, periodiccnt + 1, EMPTY2); 2055 array_needsize (WT, periodics, periodicmax, periodiccnt + HEAP0, EMPTY2);
1969 periodics [periodiccnt] = (WT)w; 2056 periodics [ev_active (w)] = (WT)w;
1970 upheap (periodics, periodiccnt); 2057 upheap (periodics, ev_active (w));
1971 2058
1972 /*assert (("internal periodic heap corruption", periodics [ev_active (w)] == w));*/ 2059 /*assert (("internal periodic heap corruption", periodics [ev_active (w)] == w));*/
1973} 2060}
1974 2061
1975void noinline 2062void noinline
1982 { 2069 {
1983 int active = ev_active (w); 2070 int active = ev_active (w);
1984 2071
1985 assert (("internal periodic heap corruption", periodics [active] == (WT)w)); 2072 assert (("internal periodic heap corruption", periodics [active] == (WT)w));
1986 2073
1987 if (expect_true (active < periodiccnt)) 2074 if (expect_true (active < periodiccnt + HEAP0 - 1))
1988 { 2075 {
1989 periodics [active] = periodics [periodiccnt]; 2076 periodics [active] = periodics [periodiccnt + HEAP0 - 1];
1990 adjustheap (periodics, periodiccnt, active); 2077 adjustheap (periodics, periodiccnt, active);
1991 } 2078 }
1992 2079
1993 --periodiccnt; 2080 --periodiccnt;
1994 } 2081 }
2114 if (w->wd < 0) 2201 if (w->wd < 0)
2115 { 2202 {
2116 ev_timer_start (EV_A_ &w->timer); /* this is not race-free, so we still need to recheck periodically */ 2203 ev_timer_start (EV_A_ &w->timer); /* this is not race-free, so we still need to recheck periodically */
2117 2204
2118 /* monitor some parent directory for speedup hints */ 2205 /* monitor some parent directory for speedup hints */
2206 /* note that exceeding the hardcoded limit is not a correctness issue, */
2207 /* but an efficiency issue only */
2119 if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096) 2208 if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096)
2120 { 2209 {
2121 char path [4096]; 2210 char path [4096];
2122 strcpy (path, w->path); 2211 strcpy (path, w->path);
2123 2212

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines