Revision f364ec65 block.c

b/block.c
3100 3100
    return data.ret;
3101 3101
}
3102 3102

  
3103
BlockInfo *bdrv_query_info(BlockDriverState *bs)
3104
{
3105
    BlockInfo *info = g_malloc0(sizeof(*info));
3106
    info->device = g_strdup(bs->device_name);
3107
    info->type = g_strdup("unknown");
3108
    info->locked = bdrv_dev_is_medium_locked(bs);
3109
    info->removable = bdrv_dev_has_removable_media(bs);
3110

  
3111
    if (bdrv_dev_has_removable_media(bs)) {
3112
        info->has_tray_open = true;
3113
        info->tray_open = bdrv_dev_is_tray_open(bs);
3114
    }
3115

  
3116
    if (bdrv_iostatus_is_enabled(bs)) {
3117
        info->has_io_status = true;
3118
        info->io_status = bs->iostatus;
3119
    }
3120

  
3121
    if (bs->dirty_bitmap) {
3122
        info->has_dirty = true;
3123
        info->dirty = g_malloc0(sizeof(*info->dirty));
3124
        info->dirty->count = bdrv_get_dirty_count(bs) * BDRV_SECTOR_SIZE;
3125
        info->dirty->granularity =
3126
            ((int64_t) BDRV_SECTOR_SIZE << hbitmap_granularity(bs->dirty_bitmap));
3127
    }
3128

  
3129
    if (bs->drv) {
3130
        info->has_inserted = true;
3131
        info->inserted = g_malloc0(sizeof(*info->inserted));
3132
        info->inserted->file = g_strdup(bs->filename);
3133
        info->inserted->ro = bs->read_only;
3134
        info->inserted->drv = g_strdup(bs->drv->format_name);
3135
        info->inserted->encrypted = bs->encrypted;
3136
        info->inserted->encryption_key_missing = bdrv_key_required(bs);
3137

  
3138
        if (bs->backing_file[0]) {
3139
            info->inserted->has_backing_file = true;
3140
            info->inserted->backing_file = g_strdup(bs->backing_file);
3141
        }
3142

  
3143
        info->inserted->backing_file_depth = bdrv_get_backing_file_depth(bs);
3144

  
3145
        if (bs->io_limits_enabled) {
3146
            info->inserted->bps =
3147
                           bs->io_limits.bps[BLOCK_IO_LIMIT_TOTAL];
3148
            info->inserted->bps_rd =
3149
                           bs->io_limits.bps[BLOCK_IO_LIMIT_READ];
3150
            info->inserted->bps_wr =
3151
                           bs->io_limits.bps[BLOCK_IO_LIMIT_WRITE];
3152
            info->inserted->iops =
3153
                           bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL];
3154
            info->inserted->iops_rd =
3155
                           bs->io_limits.iops[BLOCK_IO_LIMIT_READ];
3156
            info->inserted->iops_wr =
3157
                           bs->io_limits.iops[BLOCK_IO_LIMIT_WRITE];
3158
        }
3159
    }
3160
    return info;
3161
}
3162

  
3163
BlockInfoList *qmp_query_block(Error **errp)
3164
{
3165
    BlockInfoList *head = NULL, **p_next = &head;
3166
    BlockDriverState *bs;
3167

  
3168
    QTAILQ_FOREACH(bs, &bdrv_states, list) {
3169
        BlockInfoList *info = g_malloc0(sizeof(*info));
3170
        info->value = bdrv_query_info(bs);
3171

  
3172
        *p_next = info;
3173
        p_next = &info->next;
3174
    }
3175

  
3176
    return head;
3177
}
3178

  
3179
BlockStats *bdrv_query_stats(const BlockDriverState *bs)
3180
{
3181
    BlockStats *s;
3182

  
3183
    s = g_malloc0(sizeof(*s));
3184

  
3185
    if (bs->device_name[0]) {
3186
        s->has_device = true;
3187
        s->device = g_strdup(bs->device_name);
3188
    }
3189

  
3190
    s->stats = g_malloc0(sizeof(*s->stats));
3191
    s->stats->rd_bytes = bs->nr_bytes[BDRV_ACCT_READ];
3192
    s->stats->wr_bytes = bs->nr_bytes[BDRV_ACCT_WRITE];
3193
    s->stats->rd_operations = bs->nr_ops[BDRV_ACCT_READ];
3194
    s->stats->wr_operations = bs->nr_ops[BDRV_ACCT_WRITE];
3195
    s->stats->wr_highest_offset = bs->wr_highest_sector * BDRV_SECTOR_SIZE;
3196
    s->stats->flush_operations = bs->nr_ops[BDRV_ACCT_FLUSH];
3197
    s->stats->wr_total_time_ns = bs->total_time_ns[BDRV_ACCT_WRITE];
3198
    s->stats->rd_total_time_ns = bs->total_time_ns[BDRV_ACCT_READ];
3199
    s->stats->flush_total_time_ns = bs->total_time_ns[BDRV_ACCT_FLUSH];
3200

  
3201
    if (bs->file) {
3202
        s->has_parent = true;
3203
        s->parent = bdrv_query_stats(bs->file);
3204
    }
3205

  
3206
    return s;
3207
}
3208

  
3209
BlockStatsList *qmp_query_blockstats(Error **errp)
3210
{
3211
    BlockStatsList *head = NULL, **p_next = &head;
3212
    BlockDriverState *bs;
3213

  
3214
    QTAILQ_FOREACH(bs, &bdrv_states, list) {
3215
        BlockStatsList *info = g_malloc0(sizeof(*info));
3216
        info->value = bdrv_query_stats(bs);
3217

  
3218
        *p_next = info;
3219
        p_next = &info->next;
3220
    }
3221

  
3222
    return head;
3223
}
3224

  
3225 3103
const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
3226 3104
{
3227 3105
    if (bs->backing_hd && bs->backing_hd->encrypted)
......
3457 3335
    return curr_bs;
3458 3336
}
3459 3337

  
3460
#define NB_SUFFIXES 4
3461

  
3462
char *get_human_readable_size(char *buf, int buf_size, int64_t size)
3463
{
3464
    static const char suffixes[NB_SUFFIXES] = "KMGT";
3465
    int64_t base;
3466
    int i;
3467

  
3468
    if (size <= 999) {
3469
        snprintf(buf, buf_size, "%" PRId64, size);
3470
    } else {
3471
        base = 1024;
3472
        for(i = 0; i < NB_SUFFIXES; i++) {
3473
            if (size < (10 * base)) {
3474
                snprintf(buf, buf_size, "%0.1f%c",
3475
                         (double)size / base,
3476
                         suffixes[i]);
3477
                break;
3478
            } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
3479
                snprintf(buf, buf_size, "%" PRId64 "%c",
3480
                         ((size + (base >> 1)) / base),
3481
                         suffixes[i]);
3482
                break;
3483
            }
3484
            base = base * 1024;
3485
        }
3486
    }
3487
    return buf;
3488
}
3489

  
3490
char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
3491
{
3492
    char buf1[128], date_buf[128], clock_buf[128];
3493
    struct tm tm;
3494
    time_t ti;
3495
    int64_t secs;
3496

  
3497
    if (!sn) {
3498
        snprintf(buf, buf_size,
3499
                 "%-10s%-20s%7s%20s%15s",
3500
                 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
3501
    } else {
3502
        ti = sn->date_sec;
3503
        localtime_r(&ti, &tm);
3504
        strftime(date_buf, sizeof(date_buf),
3505
                 "%Y-%m-%d %H:%M:%S", &tm);
3506
        secs = sn->vm_clock_nsec / 1000000000;
3507
        snprintf(clock_buf, sizeof(clock_buf),
3508
                 "%02d:%02d:%02d.%03d",
3509
                 (int)(secs / 3600),
3510
                 (int)((secs / 60) % 60),
3511
                 (int)(secs % 60),
3512
                 (int)((sn->vm_clock_nsec / 1000000) % 1000));
3513
        snprintf(buf, buf_size,
3514
                 "%-10s%-20s%7s%20s%15s",
3515
                 sn->id_str, sn->name,
3516
                 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
3517
                 date_buf,
3518
                 clock_buf);
3519
    }
3520
    return buf;
3521
}
3522

  
3523 3338
/**************************************************************/
3524 3339
/* async I/Os */
3525 3340

  

Also available in: Unified diff