Example 1 - Issues

The following python script generates some issues and sends them to Code Advisor.

#!/usr/bin/env python
"""
Copyright (C) Arm Limited 2017. All rights reserved.
"""
from __future__ import print_function

# Import the api object from the module
from codeadvisor import API

# Import the 'issues' datatype
from codeadvisor.types import Issue

def gen_issues(amount):
    """
    Generate an `amount` of issues
    """
    for i in xrange(0, amount):
        # Create the issue object
        issue = Issue()
        # Give the issue a unique ID
        issue.id = "issue {}".format(i)

        # Is the issue info or advice?
        issue.type = ["info", "advice"][i % 2]
        # Add a category
        issue.categories.append("fish")
        # Give the issue a title which will appear in the UI
        issue.title = "AN ISSUE CALLED {}".format(i)
        # Set the file the issue should appear in
        issue.position.source_path = "/dev/null"

        yield issue

# Create the API object with the plugin name example-issues
# and using json encoding
plugin = API("example-issues", "ARM", "json")

# we're producing issues, so state that we are publishing to that stream
plugin.add_producer("armcadvisor:issues")

def main():
    # Setup the plugin and handle --config if it is passed to us
    plugin.setup()

    for issue in gen_issues(100):
        # Send the issues to the issue channel
        plugin.send("armcadvisor:issues", issue)


if __name__ == "__main__":
    main()

Example 2 - Samples

This python plug-in script produces perf records that are then sent to Code Advisor.

#!/usr/bin/env python
"""
Copyright (C) Arm Limited 2017. All rights reserved.

"""

from random import randint
import time

# Import the api object from the module
from codeadvisor import API

# This plugin will be working with samples so import the appropriate datatype
from codeadvisor.types import Sample

def gen_samples(amount):
    """
    Generate `amount` perf records and send them down the samples stream
    """
    for i in xrange(0, amount):
        # Create a sample object that we can fill in
        sample = Sample()

        # Fill in the common fields with some dummy values
        sample.header.cpu = randint(0, 96)
        sample.header.pid = randint(1, 256)
        sample.header.time = int(time.time())

        sample.instruction_pointer = 0x13
        yield sample

# Create the API object with the plug-in name example-samples
# and using json encoding
plugin = API("example-samples", "ARM", "json")

# Tell the API object we will be producing samples
plugin.add_producer("armcadvisor:samples")

def main():
    # Call the setup method so that we handle --config and setup the streams
    plugin.setup()

    for thing in gen_samples(100):
        # Send the generated sample to samples
        plugin.send("armcadvisor:samples", thing)


if __name__ == "__main__":
    main()

Example 3 - Consumer

This python plugin accumulates samples and issues and summarizes them by printing out the total number of each type.

#!/usr/bin/env python
"""
Copyright (C) Arm Limited 2017. All rights reserved.

"""

from __future__ import print_function

import sys

# Import the api object from the module
from codeadvisor import API

things = {}

def add(thing):
    """
    This is an accumulator we use as a callback
    it keeps track of the number of each type of sample
    """
    try:
        things[thing.__class__.__name__] += 1
    except KeyError:
        things[thing.__class__.__name__] = 1

# Init the API
plugin = API("example-summarizer", "ARM", encoding="json")

# We're interested in issues and samples so subscribe to them and add a
# function to handle what we receive
plugin.add_consumer("armcadvisor:issues", add)
plugin.add_consumer("armcadvisor:samples", add)

def main():
    # Call the setup method so that we handle --config and setup the streams
    plugin.setup()

    # Wait for all the input streams to have completed
    plugin.done()

    # stdout is reserved for the api to use, but we can always print to
    # stderr so print out what we've found
    print("Things Recieved:", file=sys.stderr)
    for key in sorted(things.keys()):
        print("{}: {}".format(key, things[key]), file=sys.stderr)



if __name__ == "__main__":
    main()