Running perf within a VirtualBox guest - linux

My professor has asked me to run:
perf stat -e mem-loads,mem-stores ./test
In order for me to find the memory loads and stores for a C file. I'm doing this from within a VirtualBox Linux guest and and problem is that perf is saying this is an invalid or unsupported event, can anyone advise as to what I'm doing wrong here?
invalid or unsupported event: 'mem-loads,'
Run 'perf list' for a list of valid events
Usage: perf stat [<options>] [<command>]
-e, --event <event> event selector. use 'perf list' to list available events
Are perf hardware events available from within a VirtualBox guest?

With a few exceptions (that don't include VirtualBox), you cannot use hardware PMU events in perf from within a virtual machine guest on Linux.
Perhaps there are lab computers that have native Linux installations, or you could use a bootable Linux flash drive to do you test. If your professor was the one who suggested a virtual machine in the first place, you could raise the issue with them.


Emulating an Arietta G25 board with QEMU

I am attempting to emulate an Arietta G25 board using QEMU but am having some difficulties.
I began by compiling Linux with patches to support Arietta, as described here. I then examined the list of supported machines with qemu-system-arm -M help and noticed that while the Arietta board was not listed, there was an option for versatileab and versatilepb which have the same processor as the Arietta.
Inside the kernel directory arch/arm/boot, I ran the command
qemu-system-arm -M versatilepb \
-m 256M \
-kernel zImage \
-dtb dts/acme-arietta.dtb \
-nographic \
-append "console=ttyAMA0"
I expected to see the kernel boot and immediately panic due to the absence of a root filesystem. However, no text was ever displayed on my console.
I tried playing around with the arguments to console (such as using ttyS0, adding rates, etc.) but that did not work.
This is a very common point of confusion for people coming to the ARM embedded world who are used to x86. For x86, essentially every bit of hardware you buy will look like a standard PC -- the memory, serial port, etc will all be in exactly the same place and accessed the same way. So guest software will generally just work on any of these systems.
ARM is very different. Different embedded boards and SoCs will have different devices present, and put them at different addresses. Software running on them needs to be built to work with those devices, and trying to run low level software (like a kernel or bootloader or firmware image) on the wrong kind of device will not work. What is happening here is that you've said "boot a kernel built for Arietta and tell it that the UART is where the Arietta's UART lives", but you're running it on a piece of emulated hardware (versatilepb) that is very very different. The kernel will try to talk to hardware that doesn't exist and will just crash. It can't print anything either because it doesn't know where the versatilepb UART is.
Adding support for emulation of a new board model to QEMU is not a trivial process (think of it as about the same amount of work as adding support for that board to the Linux kernel), as you need to write device models for all the hardware that is on that board and which the guest kernel tries to use.

Linux perf events profiling in Google Compute Engine not working

I'm new to using Google Compute Engine. I'd like to use the Linux perf tool to do some various perf events measurements of my application and eventually sample profiling. I've installed the linux perf tool on my Ubuntu 16.04 LTS VM. However even basic events like cycles show up as "not supported". I'm guessing that the underlying KVM hypervisor does not have virtual PMU support enabled, although I believe KVM does support this with a non-default flag setting. Is there any way to get this working?
# perf stat -e cycles -a sleep 10
Performance counter stats for 'system wide':
<not supported> cycles
10.000598339 seconds time elapsed
Linux perf tool by default tries to use hardware performance monitoring counters. When your OS is virtualized, you have no direct access to all counters; several virtualization solutions may allow access to some basic counters, if configured.
In your case it seems that GCE virtualization give no such access. (Ask support of your GCE? If it was disabled outside of your VM, you cant change this.)
You may use perf with some software events, for example -e task-clock to get basic profiling. Check perf stat output for supported events and perf list for Software events.
perf stat -e task-clock ...
perf record -e task-clock ...

Not able to run vnstat unless as root in debian

So, I need to run this command:
vnstat -tr
But as a user I just created, not as root, as root it works fine, but as a regular user I get this:
Error: Unable to get interface "eth0" statistics.
Error: Interface "eth0" not available, exiting.
Operating system: Debian Linux 6.0.6
The problem was grsecurity locking /proc/net/dev from viewing it by users. The solution was to downgrade from ovhs linux core from 3.10.X to 3.8.13.

How to profile an app running inside KVM guest

Is there any way to profile an application running inside KVM guest using a tool like perf_events?
I've tried to do that using
perf kvm --guestkallsyms=.. --guestmodules=.. --guest record -a
but information in report is pretty useless:
# ========
# Samples: 627 of event 'cache-misses'
# Event count (approx.): 295421
# Overhead Command Shared Object Symbol
# ........ ....... ................ ......................
73.18% :15661 [x_tables] [g] 0xffffffff8176bc80
26.82% :15661 [unknown] [u] 0x00000000004004fe
# (For a higher level overview, try: perf report --sort comm,dso)
The perf tool runs in the host and does not have any way to get information about the applications in the guest. I think the attribution of samples to guest-kernelspace or guest-userspace is based on the cpu-mode at the time the sample was taken (not on higher-level information about what the guest is doing).
You can get some profiling information by running perf directly in the guest. Use perf-list to see the options (they are probably all in the 'software' category).
Yes, you probably can.
the host can see the guest.
you can use raw hardware events to do so(just check the event number to be available on your system).
for me this works as an example
sudo perf kvm stat -I 1000 -e r1a8 -a
(make sure you are monitoring the guest by turning off the kvm machine after a while and see the zeros ..)
what about
sudo perf kvm stat record -p appPID
it should work based on the help of perf kvm but it does not! it works fine in system wide mode with -a

How to detect if the script is running on a virtual machine?

I don't think the question needs any explanation.
I'm writing a software that can boot virtual machines to simulate a network. I would like to disable this feature if the software is already running on a virtual machine.
I already read this post: How to detect if my application is running in a virtual machine?
But I need to do it on Linux and that post covers only Windows.
I'm tagging it as a Perl question as I've to do it in Perl, but probably there are some file in proc or somewhere else to check and it's the same for all languages. I don't know actually.
I'm interested in detecting mainly VirtualBox and KVM, as those are the VMs I'm using.
So, any suggestion?
I will talk specific to VMware and virtual Box Virtual Machines running Linux as guest Operating system.
If you run below command, you will come to know that the underlying hardware is VMware/VirtualBox which certifies that it is a Virtual Machine.
For VMware guest:
# dmidecode | grep -i product
Product Name: VMware Virtual Platform
For Virtual Box guest:
# dmidecode | grep -i product
Product Name: VirtualBox
"dmidecode" is a linux system command. You can have perl run dmidecode in the beginning of your script and extract the value. If it is a virtual machine then the script should exit without any further execution.
I do not have any other hypervisor at my disposal to get you what above command return on them.
Hope this helps.
A quick Google yields blog/ How to detect virtualization
Perl module and everything.
Have you tried Sys::Detect::Virtualization, available on CPAN?