コード例 #1
0
ファイル: resource.c プロジェクト: Jimlan/kbengine
static PyObject *
resource_getrusage(PyObject *self, PyObject *args)
{
    int who;
    struct rusage ru;
    PyObject *result;

    if (!PyArg_ParseTuple(args, "i:getrusage", &who))
        return NULL;

    if (getrusage(who, &ru) == -1) {
        if (errno == EINVAL) {
            PyErr_SetString(PyExc_ValueError,
                            "invalid who parameter");
            return NULL;
        }
        PyErr_SetFromErrno(ResourceError);
        return NULL;
    }

    result = PyStructSequence_New(&StructRUsageType);
    if (!result)
        return NULL;

    PyStructSequence_SET_ITEM(result, 0,
                    PyFloat_FromDouble(doubletime(ru.ru_utime)));
    PyStructSequence_SET_ITEM(result, 1,
                    PyFloat_FromDouble(doubletime(ru.ru_stime)));
    PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong(ru.ru_maxrss));
    PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong(ru.ru_ixrss));
    PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong(ru.ru_idrss));
    PyStructSequence_SET_ITEM(result, 5, PyLong_FromLong(ru.ru_isrss));
    PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(ru.ru_minflt));
    PyStructSequence_SET_ITEM(result, 7, PyLong_FromLong(ru.ru_majflt));
    PyStructSequence_SET_ITEM(result, 8, PyLong_FromLong(ru.ru_nswap));
    PyStructSequence_SET_ITEM(result, 9, PyLong_FromLong(ru.ru_inblock));
    PyStructSequence_SET_ITEM(result, 10, PyLong_FromLong(ru.ru_oublock));
    PyStructSequence_SET_ITEM(result, 11, PyLong_FromLong(ru.ru_msgsnd));
    PyStructSequence_SET_ITEM(result, 12, PyLong_FromLong(ru.ru_msgrcv));
    PyStructSequence_SET_ITEM(result, 13, PyLong_FromLong(ru.ru_nsignals));
    PyStructSequence_SET_ITEM(result, 14, PyLong_FromLong(ru.ru_nvcsw));
    PyStructSequence_SET_ITEM(result, 15, PyLong_FromLong(ru.ru_nivcsw));

    if (PyErr_Occurred()) {
        Py_DECREF(result);
        return NULL;
    }

    return result;
}
コード例 #2
0
ファイル: useinfo.c プロジェクト: augre/pegasus
int printXMLUseInfo(FILE *out, int indent, const char* id,
                    const struct rusage* use) {
    /* purpose: format the rusage record into the given stream as XML.
     * paramtr: out (IO): the stream
     *          indent (IN): indentation level
     *          id (IN): object identifier to use as element tag name.
     *          use (IN): struct rusage info
     * returns: number of characters put into buffer (buffer length)
     */
    char b[4][32];

    /* <usage> */
    fprintf(out, "%*s<%s utime=\"%.3f\" stime=\"%.3f\"", indent, "",
            id, doubletime(use->ru_utime), doubletime(use->ru_stime));

    fprintf(out, " maxrss=\"%lu\"", 
#ifdef DARWIN
            /* On Mac OS X this is in bytes */
            use->ru_maxrss / 1024
#else
            /* On linux it is in KB */
            use->ru_maxrss
#endif
    );

    fprintf(out, " minflt=\"%s\" majflt=\"%s\" nswap=\"%s\"",
            sizer(b[0], 32, sizeof(use->ru_minflt), &(use->ru_minflt)),
            sizer(b[1], 32, sizeof(use->ru_majflt), &(use->ru_majflt)),
            sizer(b[2], 32, sizeof(use->ru_nswap), &(use->ru_nswap)));

    fprintf(out, " inblock=\"%s\" outblock=\"%s\"",
            sizer(b[0], 32, sizeof(use->ru_inblock), &(use->ru_inblock)),
            sizer(b[1], 32, sizeof(use->ru_oublock), &(use->ru_oublock)));

    fprintf(out, " msgsnd=\"%s\" msgrcv=\"%s\"",
            sizer(b[2], 32, sizeof(use->ru_msgsnd), &(use->ru_msgsnd)),
            sizer(b[3], 32, sizeof(use->ru_msgrcv), &(use->ru_msgrcv)));

    fprintf(out, " nsignals=\"%s\" nvcsw=\"%s\" nivcsw=\"%s\"/>\n",
            sizer(b[0], 32, sizeof(use->ru_nsignals), &(use->ru_nsignals)),
            sizer(b[1], 32, sizeof(use->ru_nvcsw), &(use->ru_nvcsw)),
            sizer(b[2], 32, sizeof(use->ru_nivcsw), &(use->ru_nivcsw)));

    return 0;
}
コード例 #3
0
ファイル: Binrec.cpp プロジェクト: BodyTrack/datastore
void parse_bt_file(const std::string &infile,
                   std::map<std::string, simple_shared_ptr<std::vector<DataSample<double> > > > &data,
                   std::vector<ParseError> &errors,
		   ParseInfo &info)
{
  info.good_records = 0;
  info.bad_records = 0;
  double begintime = doubletime();
  // Memory-map file
  FILE *in = fopen(infile.c_str(), "rb");
  if (!in) throw std::runtime_error("fopen");
  struct stat statbuf;
  if (-1 == fstat(fileno(in), &statbuf)) throw std::runtime_error("fopen");
  long long len = statbuf.st_size;
  const unsigned char *in_mem = (unsigned char*) mmap(NULL, len, PROT_READ, MAP_SHARED/*|MAP_POPULATE*/, fileno(in), 0);
  const unsigned char *end = in_mem + len;
  if (in_mem == (unsigned char*)-1) throw std::runtime_error("mmap");
  if (verbose) log_f("parse_bt_file: Mapped %s (%lld KB)", infile.c_str(), len/1024);
  Source source(in_mem);
  const unsigned char *ptr = in_mem;

  int nrecords[256];
  memset(nrecords, 0, sizeof(nrecords));
  long long nvalues=0;
  
  StartOfFileRecord sofr;
  TickToTime ttt;

  std::map<std::string, unsigned long long> last_tick;
  bool out_of_order = false;
  
  while (ptr < end) {
    const unsigned char *beginning_of_record = ptr;
    try {
      unsigned int magic = read_u32(ptr);
      if (magic != 0xb0de744c) throw ParseError("Incorrect magic # at byte %d", source.pos(ptr - 4));
      unsigned int record_size = read_u32(ptr);
      if (verbose) {
        log_f("parse_bt_file:  At location %d, magic=0x%x, record size %d",
              (int)(beginning_of_record - in_mem), magic, record_size);
      }
      if (record_size + beginning_of_record > end) throw ParseError("Record size too long at byte %d (size=%d, but only %d bytes left in file)", source.pos(ptr - 4), record_size, end-beginning_of_record);
      int record_type = read_u16(ptr);
      if (record_type != RTYPE_START_OF_FILE && record_type != RTYPE_RTC && record_type != RTYPE_PERIODIC_DATA) {
        throw ParseError("Unknown record type 0x%x at byte %d", record_type, source.pos(ptr - 2));
      }
      const unsigned char *payload = ptr;
      unsigned int payload_len = record_size-14;
      ptr += payload_len;
      if (verbose) log_f("parse_bt_file: Got record type %d, payload len %d", record_type, payload_len);
      unsigned int crc = read_u32(ptr);
      unsigned int calculated_crc = crc32(beginning_of_record, record_size - 4, 0);
      if (crc != calculated_crc) {
        // Recoverable error;  add to errors and try to continue
        errors.push_back(ParseError("Incorrect CRC32 byte %d.  read 0x%x != calculated 0x%x",
                                    source.pos(ptr - 4), crc, calculated_crc));
	if (record_type == RTYPE_PERIODIC_DATA) info.bad_records++;
        continue;
      }

      switch (record_type) {
      case RTYPE_START_OF_FILE:
        sofr = StartOfFileRecord(source, payload, payload_len);
        ttt.receive_binrec(sofr);
	info.channel_specs = sofr.channel_specs;
        break;
      case RTYPE_RTC:
      {
        RtcRecord rtcr(source, payload, payload_len);
        ttt.receive_binrec(rtcr);
        if (verbose) log_f("parse_bt_file: %s", rtcr.to_string().c_str());
      }
      break;
      case RTYPE_PERIODIC_DATA:
      {
        PeriodicDataRecord pdr(source, payload, payload_len, &sofr);
        ttt.receive_binrec(pdr);
        pdr.set_time(ttt);
        for (unsigned i = 0; i < pdr.n_channels(); i++) {
          std::string channel_name = pdr.channel_name(i);
          nvalues += pdr.number_of_samples;
          log_f("parse_pt_file: %d samples, start tick 0x%x (%u)",
                pdr.number_of_samples, pdr.first_sample_short_tick, pdr.first_sample_short_tick);
          std::vector<DataSample<double> > data_samples;
          pdr.get_data_samples(i, data_samples);

          if (data_samples.size()) {
            if (data.find(channel_name) == data.end()) {
              data[channel_name].reset(new std::vector<DataSample<double> >());
            } else {
              if (data[channel_name]->back().time > data_samples.front().time) {
                if (verbose) log_f("Warning: sample times in channel %s are out-of-order (%f > %f)",
                                   channel_name.c_str(),
                                   data[channel_name]->back().time, data_samples.front().time);
                out_of_order = true;
              }
            }
            
            data[channel_name]->insert(data[channel_name]->end(), data_samples.begin(), data_samples.end());
	    info.good_records++;
          }
          last_tick[channel_name] = pdr.first_sample_long_tick;
          
          
        }
      }
      break;
      default:
        assert(0);
      }
      nrecords[record_type]++;
    }
    catch (ParseError &e) {
      errors.push_back(ParseError("In record starting at byte %d in file %s: %s",
                                  source.pos(beginning_of_record), infile.c_str(), e.what()));
      info.bad_records++;
    }
  }
  
  if (-1 == munmap((void*)in_mem, len)) { perror("munmap"); exit(1); }
  fclose(in);
  
  if (out_of_order) {
    for (std::map<std::string, simple_shared_ptr<std::vector<DataSample<double> > > >::iterator i =
           data.begin(); i != data.end(); ++i) {
      
      simple_shared_ptr<std::vector<DataSample<double > > > samples = i->second;
      std::sort(samples->begin(), samples->end(), DataSample<double>::time_lessthan);
    }
  }

  // Check samples are in order
  for (std::map<std::string, simple_shared_ptr<std::vector<DataSample<double> > > >::iterator i =
         data.begin(); i != data.end(); ++i) {
    
    simple_shared_ptr<std::vector<DataSample<double > > > samples = i->second;
    for (unsigned i = 0; i < samples->size()-1; i++) {
      assert((*samples)[i].time <= ((*samples)[i+1].time));
    }
  }

  double duration = doubletime() - begintime;
  log_f("parse_bt_file: Parsed %lld bytes in %g seconds (%dK/sec)", len, duration, (int)(len / duration / 1024));
  if (verbose) {
    log_f("parse_bt_file: %d RTYPE_START_OF_FILE records", nrecords[RTYPE_START_OF_FILE]);
    log_f("parse_bt_file: %d RTYPE_RTC records", nrecords[RTYPE_RTC]);
    log_f("parse_bt_file: %d RTYPE_PERIODIC_DATA records", nrecords[RTYPE_PERIODIC_DATA]);
    log_f("parse_bt_file:    %lld values", nvalues);
  }
}
コード例 #4
0
ファイル: jobinfo.c プロジェクト: kyo19/pegasus
int
printXMLJobInfo(FILE *out, int indent, const char* tag, 
                const JobInfo* job)
/* purpose: format the job information into the given stream as XML.
 * paramtr: out (IO): the stream
 *          indent (IN): indentation level
 *          tag (IN): name to use for element tags.
 *          job (IN): job info to print.
 * returns: number of characters put into buffer (buffer length)
 */
{
  int status;        /* $#@! broken Debian headers */

  /* sanity check */
  if (!job->isValid) return 0;

  /* start tag with indentation */
  fprintf(out, "%*s<%s start=\"%s\"", indent, "", tag,
          fmtisodate(isLocal, isExtended, job->start.tv_sec,
                     job->start.tv_usec));
  fprintf(out, " duration=\"%.3f\"",
          doubletime(job->finish) - doubletime(job->start));

  /* optional attribute: application process id */
  if (job->child != 0)
    fprintf(out, " pid=\"%d\"", job->child);

  /* finalize open tag of element */
  fprintf(out, ">\n");

  /* <usage> */
  printXMLUseInfo(out, indent+2, "usage", &job->use);

  /* <status>: open tag */
  fprintf(out, "%*s<status raw=\"%d\">", indent+2, "", job->status);

  /* <status>: cases of completion */
  status = (int) job->status;        /* $#@! broken Debian headers */
  if (job->status < 0) {
    /* <failure> */
    fprintf(out, "<failure error=\"%d\">%s%s</failure>", job->saverr,
            job->prefix && job->prefix[0] ? job->prefix : "",
            strerror(job->saverr));
  } else if (WIFEXITED(status)) {
    fprintf(out, "<regular exitcode=\"%d\"/>", WEXITSTATUS(status));
  } else if (WIFSIGNALED(status)) {
    /* result = 128 + WTERMSIG(status); */
    fprintf(out, "<signalled signal=\"%u\"", WTERMSIG(status));
#ifdef WCOREDUMP
    fprintf(out, " corefile=\"%s\"", WCOREDUMP(status) ? "true" : "false");
#endif
    fprintf(out, ">%s</signalled>",
#if defined(CYGWINNT50) || defined(CYGWINNT51)
             "unknown"
#else
             sys_siglist[WTERMSIG(status)]
#endif
    );
  } else if (WIFSTOPPED(status)) {
    fprintf(out, "<suspended signal=\"%u\">%s</suspended>", WSTOPSIG(status),
#if defined(CYGWINNT50) || defined(CYGWINNT51)
             "unknown"
#else
             sys_siglist[WSTOPSIG(status)]
#endif
    );
  } /* FIXME: else? */
  fprintf(out, "</status>\n");

  /* <executable> */
  printXMLStatInfo(out, indent+2, "statcall", NULL, &job->executable);

#ifdef WITH_NEW_ARGS
  /* alternative 1: new-style <argument-vector> */
  fprintf(out, "%*s<argument-vector", indent+2, "");
  if (job->argc == 1) {
    /* empty element */
    fprintf(out, "/>\n");
  } else {
    /* content are the CLI args */
    int i;

    fprintf(out, ">\n");
    for (i=1; i<job->argc; ++i) {
      fprintf(out, "%*s<arg nr=\"%d\">", indent+4, "", i);
      xmlquote(out, job->argv[i], strlen(job->argv[i]));
      fprintf(out, "</arg>\n");
    }

    /* end tag */
    fprintf(out, "%*s</argument-vector>\n", indent+2, "");
  }
#else
  /* alternative 2: old-stlye <arguments> */
  fprintf(out, "%*s<arguments", indent+2, "");
  if (job->argc == 1) {
    /* empty element */
    fprintf(out, "/>\n");
  } else {
    /* content are the CLI args */
    int i = 1;

    fprintf(out, ">");
    while (i < job->argc) {
      xmlquote(out, job->argv[i], strlen(job->argv[i]));
      if (++i < job->argc) fprintf(out, " ");
    }

    /* end tag */
    fprintf(out, "</arguments>\n");
  }
#endif /* WITH_NEW_ARGS */

  /* <proc>s */
  printXMLProcInfo(out, indent+2, job->children);

  /* finalize close tag of outmost element */
  fprintf(out, "%*s</%s>\n", indent, "", tag);

  return 0;
}
コード例 #5
0
ファイル: jobinfo.c プロジェクト: pegasus-isi/pegasus
int printYAMLJobInfo(FILE *out, int indent, const char* tag, const JobInfo* job) {
    /* purpose: format the job information into the given stream as YAML.
     * paramtr: out (IO): the stream
     *          indent (IN): indentation level
     *          tag (IN): name to use for element tags.
     *          job (IN): job info to print.
     * returns: number of characters put into buffer (buffer length)
     */

    /* sanity check */
    if (!job->isValid) {
        return 0;
    }

    /* start tag with indentation */
    fprintf(out, "%*s%s:\n", indent, "", tag);
    fprintf(out, "%*s  start: %s\n", indent, "", 
            fmtisodate(job->start.tv_sec, job->start.tv_usec));
    fprintf(out, "%*s  duration: %.3f\n", indent, "", 
            doubletime(job->finish) - doubletime(job->start));

    /* optional attribute: application process id */
    if (job->child != 0) {
        fprintf(out, "%*s  pid: %d\n", indent, "", job->child);
    }

    /* <usage> */
    printYAMLUseInfo(out, indent+2, "usage", &job->use);

    int status = (int) job->status;

    /* <status>: open tag */
    fprintf(out, "%*sstatus:\n%*sraw: %d\n",
                 indent+2, "", indent+4, "", status);

    /* <status>: cases of completion */
    if (status < 0) {
        /* <failure> */
        fprintf(out, "%*sfailure_error: %d   %s%s\n", indent+4, "",
                     job->saverr,
                     job->prefix && job->prefix[0] ? job->prefix : "",
                     strerror(job->saverr));
    } else if (WIFEXITED(status)) {
        fprintf(out, "%*sregular_exitcode: %d\n", indent+4, "",
                     WEXITSTATUS(status));
    } else if (WIFSIGNALED(status)) {
        /* result = 128 + WTERMSIG(status); */
        fprintf(out, "%*ssignalled_signal: %u\n", indent+4, "",
                     WTERMSIG(status));
        fprintf(out, "%*ssingalled_name: %s\n", indent+4, "",
                     sys_siglist[WTERMSIG(status)]);
#ifdef WCOREDUMP
        fprintf(out, "%*scorefile: %s\n", indent+4, "",
                     WCOREDUMP(status) ? "true" : "false");
#endif
    } else if (WIFSTOPPED(status)) {
        fprintf(out, "%*ssuspended_signal: %u\n", indent+4, "",
                     WSTOPSIG(status));
        fprintf(out, "%*ssuspended_name: %s\n", indent+4, "",
                sys_siglist[WSTOPSIG(status)]);
    } /* FIXME: else? */

    /* <executable> */
    printYAMLStatInfo(out, indent+2, "executable", &job->executable, 1, 0, 1);

    /* alternative 1: new-style <argument-vector> */
    fprintf(out, "%*sargument_vector:\n", indent+2, "");
    if (job->argc > 1) {
        /* content are the CLI args */
        int i;
        for (i=1; i<job->argc; ++i) {
            fprintf(out, "%*s- %s\n", indent+4, "", job->argv[i]);
        }
    }

    /* <proc>s */
    printYAMLProcInfo(out, indent+2, job->children);

    return 0;
}
コード例 #6
0
$NetBSD$

--- Modules/resource.c.orig	2012-04-09 23:07:34.024379392 +0000
+++ Modules/resource.c
@@ -86,6 +86,7 @@ resource_getrusage(PyObject *self, PyObj
                     PyFloat_FromDouble(doubletime(ru.ru_utime)));
     PyStructSequence_SET_ITEM(result, 1,
                     PyFloat_FromDouble(doubletime(ru.ru_stime)));
+#ifndef __HAIKU__
     PyStructSequence_SET_ITEM(result, 2, PyInt_FromLong(ru.ru_maxrss));
     PyStructSequence_SET_ITEM(result, 3, PyInt_FromLong(ru.ru_ixrss));
     PyStructSequence_SET_ITEM(result, 4, PyInt_FromLong(ru.ru_idrss));
@@ -100,6 +101,7 @@ resource_getrusage(PyObject *self, PyObj
     PyStructSequence_SET_ITEM(result, 13, PyInt_FromLong(ru.ru_nsignals));
     PyStructSequence_SET_ITEM(result, 14, PyInt_FromLong(ru.ru_nvcsw));
     PyStructSequence_SET_ITEM(result, 15, PyInt_FromLong(ru.ru_nivcsw));
+#endif
 
     if (PyErr_Occurred()) {
         Py_DECREF(result);