Changeset 70e2b2d in mainline for kernel/generic/src/sysinfo/stats.c


Ignore:
Timestamp:
2010-04-18T10:23:15Z (14 years ago)
Author:
Martin Decky <martin@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
c3d4bb45
Parents:
da98918
Message:

avoid costly allocation and generation of data when it is actually not necessary

File:
1 edited

Legend:

Unmodified
Added
Removed
  • kernel/generic/src/sysinfo/stats.c

    rda98918 r70e2b2d  
    8585/** Get statistics of all CPUs
    8686 *
    87  * @param item Sysinfo item (unused).
    88  * @param size Size of the returned data.
     87 * @param item    Sysinfo item (unused).
     88 * @param size    Size of the returned data.
     89 * @param dry_run Do not get the data, just calculate the size.
    8990 *
    9091 * @return Data containing several stats_cpu_t structures.
     
    9293 *         in the context of the sysinfo request.
    9394 */
    94 static void *get_stats_cpus(struct sysinfo_item *item, size_t *size)
    95 {
     95static void *get_stats_cpus(struct sysinfo_item *item, size_t *size,
     96    bool dry_run)
     97{
     98        *size = sizeof(stats_cpu_t) * config.cpu_count;
     99        if (dry_run)
     100                return NULL;
     101       
    96102        /* Assumption: config.cpu_count is constant */
    97         stats_cpu_t *stats_cpus =
    98             (stats_cpu_t *) malloc(sizeof(stats_cpu_t) * config.cpu_count,
    99             FRAME_ATOMIC);
     103        stats_cpu_t *stats_cpus = (stats_cpu_t *) malloc(*size, FRAME_ATOMIC);
    100104        if (stats_cpus == NULL) {
    101105                *size = 0;
     
    120124        interrupts_restore(ipl);
    121125       
    122         *size = sizeof(stats_cpu_t) * config.cpu_count;
    123126        return ((void *) stats_cpus);
    124127}
     
    172175/** Get task IDs
    173176 *
    174  * @param item Sysinfo item (unused).
    175  * @param size Size of the returned data.
     177 * @param item    Sysinfo item (unused).
     178 * @param size    Size of the returned data.
     179 * @param dry_run Do not get the data, just calculate the size.
    176180 *
    177181 * @return Data containing task IDs of all tasks.
     
    179183 *         in the context of the sysinfo request.
    180184 */
    181 static void *get_stats_tasks(struct sysinfo_item *item, size_t *size)
     185static void *get_stats_tasks(struct sysinfo_item *item, size_t *size,
     186    bool dry_run)
    182187{
    183188        /* Messing with task structures, avoid deadlock */
     
    198203        }
    199204       
    200         task_id_t *task_ids =
    201             (task_id_t *) malloc(sizeof(task_id_t) * count, FRAME_ATOMIC);
     205        *size = sizeof(task_id_t) * count;
     206        if (dry_run) {
     207                spinlock_unlock(&tasks_lock);
     208                interrupts_restore(ipl);
     209                return NULL;
     210        }
     211       
     212        task_id_t *task_ids = (task_id_t *) malloc(*size, FRAME_ATOMIC);
    202213        if (task_ids == NULL) {
    203214                /* No free space for allocation */
     
    216227        interrupts_restore(ipl);
    217228       
    218         *size = sizeof(task_id_t) * count;
    219229        return ((void *) task_ids);
    220230}
     
    326336/** Get physical memory statistics
    327337 *
    328  * @param item Sysinfo item (unused).
    329  * @param size Size of the returned data.
     338 * @param item    Sysinfo item (unused).
     339 * @param size    Size of the returned data.
     340 * @param dry_run Do not get the data, just calculate the size.
    330341 *
    331342 * @return Data containing stats_physmem_t.
     
    333344 *         in the context of the sysinfo request.
    334345 */
    335 static void *get_stats_physmem(struct sysinfo_item *item, size_t *size)
    336 {
     346static void *get_stats_physmem(struct sysinfo_item *item, size_t *size,
     347    bool dry_run)
     348{
     349        *size = sizeof(stats_physmem_t);
     350        if (dry_run)
     351                return NULL;
     352       
    337353        stats_physmem_t *stats_physmem =
    338             (stats_physmem_t *) malloc(sizeof(stats_physmem_t), FRAME_ATOMIC);
     354            (stats_physmem_t *) malloc(*size, FRAME_ATOMIC);
    339355        if (stats_physmem == NULL) {
    340356                *size = 0;
     
    345361            &(stats_physmem->used), &(stats_physmem->free));
    346362       
    347         *size = sizeof(stats_physmem_t);
    348363        return ((void *) stats_physmem);
    349364}
     
    351366/** Get system load
    352367 *
    353  * @param item Sysinfo item (unused).
    354  * @param size Size of the returned data.
     368 * @param item    Sysinfo item (unused).
     369 * @param size    Size of the returned data.
     370 * @param dry_run Do not get the data, just calculate the size.
    355371 *
    356372 * @return Data several load_t values.
     
    358374 *         in the context of the sysinfo request.
    359375 */
    360 static void *get_stats_load(struct sysinfo_item *item, size_t *size)
    361 {
    362         load_t *stats_load =
    363             (load_t *) malloc(sizeof(load_t) * LOAD_STEPS, FRAME_ATOMIC);
     376static void *get_stats_load(struct sysinfo_item *item, size_t *size,
     377    bool dry_run)
     378{
     379        *size = sizeof(load_t) * LOAD_STEPS;
     380        if (dry_run)
     381                return NULL;
     382       
     383        load_t *stats_load = (load_t *) malloc(*size, FRAME_ATOMIC);
    364384        if (stats_load == NULL) {
    365385                *size = 0;
     
    378398        interrupts_restore(ipl);
    379399       
    380         *size = sizeof(load_t) * LOAD_STEPS;
    381400        return ((void *) stats_load);
    382401}
Note: See TracChangeset for help on using the changeset viewer.