memstress.c (07b4b2f4047f600ca7974797900b7409081f826c) memstress.c (de10b798055db5df93474cdfa5dbffc57169f458)
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2020, Google LLC.
4 */
5#define _GNU_SOURCE
6
7#include <inttypes.h>
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2020, Google LLC.
4 */
5#define _GNU_SOURCE
6
7#include <inttypes.h>
8#include <linux/bitmap.h>
8
9#include "kvm_util.h"
10#include "memstress.h"
11#include "processor.h"
12
13struct memstress_args memstress_args;
14
15/*

--- 302 unchanged lines hidden (view full) ---

318{
319 int i;
320
321 WRITE_ONCE(memstress_args.stop_vcpus, true);
322
323 for (i = 0; i < nr_vcpus; i++)
324 pthread_join(vcpu_threads[i].thread, NULL);
325}
9
10#include "kvm_util.h"
11#include "memstress.h"
12#include "processor.h"
13
14struct memstress_args memstress_args;
15
16/*

--- 302 unchanged lines hidden (view full) ---

319{
320 int i;
321
322 WRITE_ONCE(memstress_args.stop_vcpus, true);
323
324 for (i = 0; i < nr_vcpus; i++)
325 pthread_join(vcpu_threads[i].thread, NULL);
326}
327
328static void toggle_dirty_logging(struct kvm_vm *vm, int slots, bool enable)
329{
330 int i;
331
332 for (i = 0; i < slots; i++) {
333 int slot = MEMSTRESS_MEM_SLOT_INDEX + i;
334 int flags = enable ? KVM_MEM_LOG_DIRTY_PAGES : 0;
335
336 vm_mem_region_set_flags(vm, slot, flags);
337 }
338}
339
340void memstress_enable_dirty_logging(struct kvm_vm *vm, int slots)
341{
342 toggle_dirty_logging(vm, slots, true);
343}
344
345void memstress_disable_dirty_logging(struct kvm_vm *vm, int slots)
346{
347 toggle_dirty_logging(vm, slots, false);
348}
349
350void memstress_get_dirty_log(struct kvm_vm *vm, unsigned long *bitmaps[], int slots)
351{
352 int i;
353
354 for (i = 0; i < slots; i++) {
355 int slot = MEMSTRESS_MEM_SLOT_INDEX + i;
356
357 kvm_vm_get_dirty_log(vm, slot, bitmaps[i]);
358 }
359}
360
361void memstress_clear_dirty_log(struct kvm_vm *vm, unsigned long *bitmaps[],
362 int slots, uint64_t pages_per_slot)
363{
364 int i;
365
366 for (i = 0; i < slots; i++) {
367 int slot = MEMSTRESS_MEM_SLOT_INDEX + i;
368
369 kvm_vm_clear_dirty_log(vm, slot, bitmaps[i], 0, pages_per_slot);
370 }
371}
372
373unsigned long **memstress_alloc_bitmaps(int slots, uint64_t pages_per_slot)
374{
375 unsigned long **bitmaps;
376 int i;
377
378 bitmaps = malloc(slots * sizeof(bitmaps[0]));
379 TEST_ASSERT(bitmaps, "Failed to allocate bitmaps array.");
380
381 for (i = 0; i < slots; i++) {
382 bitmaps[i] = bitmap_zalloc(pages_per_slot);
383 TEST_ASSERT(bitmaps[i], "Failed to allocate slot bitmap.");
384 }
385
386 return bitmaps;
387}
388
389void memstress_free_bitmaps(unsigned long *bitmaps[], int slots)
390{
391 int i;
392
393 for (i = 0; i < slots; i++)
394 free(bitmaps[i]);
395
396 free(bitmaps);
397}