コード例 #1
0
void STATS_ITEM::sample(double v, bool collecting_stats, double now) {
#ifdef SAMPLE_DEBUG
    switch (kind) {
    case DISK:
        printf("%s: %s: %fGB -> %fGB\n", now_str(), name, value/1e9, v/1e9);
        break;
    case NETWORK:
        printf("%s: %s: %fMbps -> %fMbps\n", now_str(), name, value/1e6, v/1e6);
        break;
    case FAULT_TOLERANCE:
        printf("%s: %s: %.0f -> %.0f\n", now_str(), name, value, v);
        break;
    }
#endif
    double old_val = value;
    value = v;
    if (!collecting_stats) return;
    if (first) {
        first = false;
        prev_t = now;
        return;
    }
    double dt = now - prev_t;
    prev_t = now;
    integral += dt*old_val;
    switch (kind) {
    case DISK:
    case NETWORK:
        if (v > extreme_val) {
            extreme_val = v;
            extreme_val_time = now;
        }
        break;
    case FAULT_TOLERANCE:
        if (v < extreme_val) {
            extreme_val = v;
            extreme_val_time = now;
        }
        break;
    }

    fprintf(f, "%f %f\n", now, old_val);
    fprintf(f, "%f %f\n", now, v);
}
OSCL_EXPORT_REF bool parse_npt_format(const char* start_ptr, const char *end_ptr,
                                      NptTimeFormat& npt_range)
{

    // get required HH:MM:SS values
    const char *sptr, *eptr;
    uint32 tmp;

    sptr = start_ptr;

    StrPtrLen now_str("now");
    if (!oscl_strncmp(sptr, now_str.c_str(), now_str.length()))
    {
        // this is the "now" keyword
        npt_range.npt_format = NptTimeFormat::NOW;
        return true;
    }

    // see if the format contains a ':' separator character
    for (eptr = sptr; eptr < end_ptr && *eptr != ':'; ++eptr);

    if (*eptr == ':')
    {
        // this is the npt-hhmmss format

        char sep = ':';
        // get the number of hours
        sptr = parse_range_integer(sptr, eptr,
                                   0, &sep, tmp);

        if (!sptr)
        {
            return false;
        }

        npt_range.npt_format = NptTimeFormat::NPT_HHMMSS;
        npt_range.npt_hhmmss.hours = tmp;


        // get the number of minutes
        sptr = parse_range_integer(sptr + 1, end_ptr,
                                   2, &sep, tmp);

        if (!sptr || *sptr != ':')
        {
            return false;
        }

        if (tmp > 59)
        {
            return false;
        }

        npt_range.npt_hhmmss.min = (uint8)tmp;


        sep = '.';
        // get the number of seconds
        sptr = parse_range_integer(sptr + 1, end_ptr,
                                   2, &sep, tmp);

        if (!sptr)
        {
            return false;
        }

        if (tmp > 59)
        {
            return false;
        }

        npt_range.npt_hhmmss.sec = (uint8)tmp;

        npt_range.npt_hhmmss.frac_sec = 0;
        // determine if the fractional seconds exists
        if (*sptr == '.')
        {
            // get the fractional seconds
            const int MAX_TMP_BUFSIZE = 12;
            char tmpbuf[MAX_TMP_BUFSIZE];
            int copy_size;

            eptr = skip_to_whitespace(sptr, end_ptr);

            copy_size = eptr - sptr;
            if (copy_size > MAX_TMP_BUFSIZE - 1)
            {
                copy_size = MAX_TMP_BUFSIZE - 1;
            }

            oscl_strncpy(tmpbuf, sptr, copy_size);

            tmpbuf[copy_size] = '\0';

            if (!PV_atof(tmpbuf, npt_range.npt_hhmmss.frac_sec))
                return false;

        }

    } // end if the format is NPT_HHMMSS

    else
    {

        char sep = '.';
        // this is the NPT_SEC format
        npt_range.npt_format = NptTimeFormat::NPT_SEC;

        // get the number of seconds
        sptr = parse_range_integer(sptr, eptr,
                                   0, &sep, tmp);

        if (!sptr)
        {
            return false;
        }

        npt_range.npt_sec.sec = tmp;

        npt_range.npt_sec.milli_sec = 0;
        if (*sptr == '.')
        {
            // there is an optional fractional seconds field

            // get the fractional seconds
            const int MAX_TMP_BUFSIZE = 12;
            char tmpbuf[MAX_TMP_BUFSIZE];
            int copy_size;

            eptr = skip_to_whitespace(sptr, end_ptr);

            copy_size = eptr - sptr;

            if (copy_size > MAX_TMP_BUFSIZE - 1)
            {
                copy_size = MAX_TMP_BUFSIZE - 1;
            }

            oscl_strncpy(tmpbuf, sptr, copy_size);


            tmpbuf[copy_size] = '\0';

            OsclFloat tmp_fnum;
            if (!PV_atof(tmpbuf, tmp_fnum))
                return false;
            npt_range.npt_sec.milli_sec = (uint32)(1000.0 * tmp_fnum + 0.5);
        }

    } // end if the format is NPT_SEC

    return true;
}
OSCL_EXPORT_REF bool compose_range_string(char *str, unsigned int max_len,
        const NptTimeFormat& npt_range,
        int& len_used)
{
    len_used = 0;
    int length;


    switch (npt_range.npt_format)
    {
        case NptTimeFormat::NOW:
        {
            StrPtrLen now_str("now");
            if ((int)max_len < now_str.length())
            {
                return false;
            }
            oscl_memcpy(str, now_str.c_str(), now_str.length());
            str += now_str.length();
            len_used += now_str.length();
            max_len -= now_str.length();
            break;
        }

        case NptTimeFormat::NPT_SEC:
        {
            char tmpstr[MAX_RANGE_INT_SIZE + 1];
            length = oscl_snprintf(tmpstr, MAX_RANGE_INT_SIZE + 1, "%d",
                                   npt_range.npt_sec.sec);
            if (length < 0 || length > MAX_RANGE_INT_SIZE)
            {
                return false;
            }
            if (length > (int) max_len)
            {
                return false;
            }
            oscl_memcpy(str, tmpstr, length);
            str += length;
            len_used += length;
            max_len -= length;
            if (npt_range.npt_sec.milli_sec > 0.0)
            {
                if (npt_range.npt_sec.milli_sec >= 1.0)
                {
                    return false;
                }
                char tmp[MAX_RANGE_FLOAT_SIZE + 2];
                length = oscl_snprintf(tmp, MAX_RANGE_FLOAT_SIZE + 2, RANGE_FLOAT_FORMAT,
                                       npt_range.npt_sec.milli_sec);
                if (length < 0 || length > MAX_RANGE_FLOAT_SIZE + 1)
                {
                    return false;
                }
                if (length > (int)max_len || tmp[1] != '.')
                {
                    return false;
                }

                oscl_memcpy(str, tmp + 1, length - 1);
                str += length - 1;
                len_used += length - 1;
                max_len -= (length - 1);
            }

            break;
        }

        case NptTimeFormat::NPT_HHMMSS:
        {
            char tmpstr[MAX_RANGE_INT_SIZE + 1];
            length = oscl_snprintf(tmpstr, MAX_RANGE_INT_SIZE + 1, "%d",
                                   npt_range.npt_hhmmss.hours);
            if (length < 0 || length >= MAX_RANGE_INT_SIZE)
            {
                return false;
            }
            if (length > (int) max_len)
            {
                return false;
            }
            oscl_memcpy(str, tmpstr, length);
            str += length;
            len_used += length;
            max_len -= length;

            if (max_len < 6)
            {
                return false;
            }

            length = oscl_snprintf(tmpstr, MAX_RANGE_INT_SIZE + 1, ":%02d:%02d",
                                   npt_range.npt_hhmmss.min, npt_range.npt_hhmmss.sec);
            if (length != 6)
            {
                return false;
            }

            oscl_memcpy(str, tmpstr, length);
            str += length;
            len_used += length;
            max_len -= length;


            if (npt_range.npt_hhmmss.frac_sec > 0.0)
            {
                if (npt_range.npt_hhmmss.frac_sec >= 1.0)
                {
                    return false;
                }
                char tmp[MAX_RANGE_FLOAT_SIZE + 2];
                length = oscl_snprintf(tmp, MAX_RANGE_FLOAT_SIZE + 2, RANGE_FLOAT_FORMAT,
                                       npt_range.npt_hhmmss.frac_sec);
                if (length < 0 || length > MAX_RANGE_FLOAT_SIZE + 1)
                {
                    return false;
                }
                if (length > (int) max_len || tmp[1] != '.')
                {
                    return false;
                }

                oscl_memcpy(str, tmp + 1, length - 1);
                str += length - 1;
                len_used += length - 1;
                max_len -= (length - 1);
            }

            break;
        }

    }

    return true;

}