Skip to content

Commit

Permalink
addon/profile/ added
Browse files Browse the repository at this point in the history
A software profiling library with sample program.
  • Loading branch information
rsta2 committed Feb 12, 2020
1 parent bf62b44 commit bcf5216
Show file tree
Hide file tree
Showing 23 changed files with 2,250 additions and 0 deletions.
1 change: 1 addition & 0 deletions addon/README
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Properties Library providing access to configuration properties saved in a file
qemu Support library and demos for using Circle with QEMU
linux Linux kernel device driver emulation (used by HDMI sound and accelerated graphics)
littlevgl LittlevGL embedded GUI library (by Gabor Kiss-Vamosi)
profile Software profiling library for performance analysis
rtc Library providing drivers for real-time clocks (RTC)
SDCard Driver for SD card access using the internal EMMC controller (by John Cronin)
sensor Drivers for I2C and other sensor devices
Expand Down
16 changes: 16 additions & 0 deletions addon/profile/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#
# Makefile
#

CIRCLEHOME = ../..

OBJS = profiler.o gmon.o mcount.o profil.o arm-mcount.o glibc_compat.o

libprofile.a: $(OBJS)
@echo " AR $@"
@rm -f $@
@$(AR) cr $@ $(OBJS)

include $(CIRCLEHOME)/Rules.mk

-include $(DEPS)
55 changes: 55 additions & 0 deletions addon/profile/arm-mcount.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/* Implementation of profiling support.
Copyright (C) 2008-2020 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library. If not, see
<https://www.gnu.org/licenses/>. */

.text

#if AARCH == 32

/* Use an assembly stub with a special ABI. The calling lr has been
pushed to the stack (which will be misaligned). We should preserve
all registers except ip and pop a word off the stack.
NOTE: This assumes mcount_internal does not clobber any non-core
(coprocessor) registers. Currently this is true, but may require
additional attention in the future.
The calling sequence looks something like:
func:
push {lr}
bl __gnu_mcount_nc
<function body>
*/
.globl __gnu_mcount_nc
__gnu_mcount_nc:
push {r0-r3, lr}
bic r1, lr, #1 /* selfpc */
ldr r0, [sp, #20] /* frompc has been pushed to stack */
bl __mcount_internal
pop {r0-r3, ip, lr}
bx ip

#else

.globl _mcount
_mcount: /* frompc is in x0 on entry */
mov x1, x30 /* selfpc */
b __mcount_internal

#endif

/* End */
181 changes: 181 additions & 0 deletions addon/profile/glibc_compat.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
//
// glibc_compat.cpp
//
// Circle - A C++ bare metal environment for Raspberry Pi
// Copyright (C) 2020 R. Stange <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
#include <profile/glibc_compat.h>
#include <circle/logger.h>
#include <circle/util.h>
#include <assert.h>

#define FATFS_FILE_DESCRIPTOR 1 // only one open file supported

static CFATFileSystem *s_pFileSystem = 0;

static FATFS *s_pFATFS = 0;
static char s_FATFSDrive[10];
static FIL *s_pFile = 0;

static const char From[] = "prof";

void __set_nocancel_filesystem (CFATFileSystem *fs)
{
assert (fs != 0);
s_pFileSystem = fs;
}

void __set_nocancel_filesystem (FATFS *fs, const char *drive)
{
assert (fs != 0);
s_pFATFS = fs;

assert (drive != 0);
strncpy (s_FATFSDrive, drive, sizeof s_FATFSDrive);
s_FATFSDrive[sizeof s_FATFSDrive-1] = '\0';
}

int __open_nocancel (const char *name, unsigned mode, unsigned umask)
{
assert (name != 0);

if ((mode & (O_CREAT | O_TRUNC)) != (O_CREAT | O_TRUNC))
{
return -1;
}

if (s_pFileSystem != 0)
{
unsigned hFile = s_pFileSystem->FileCreate (name);

return hFile > 0 ? (int) hFile : -1;
}
else
{
assert (s_pFATFS != 0);

char path[80];
if (strlen (s_FATFSDrive) + strlen (name) > sizeof path-1)
{
return -1;
}

strcpy (path, s_FATFSDrive);
strcat (path, name);

if (s_pFile != 0)
{
return -1;
}

s_pFile = new FIL;
assert (s_pFile != 0);

if (f_open (s_pFile, path, FA_WRITE | FA_CREATE_ALWAYS) == FR_OK)
{
return FATFS_FILE_DESCRIPTOR;
}
else
{
delete s_pFile;
s_pFile = 0;

return -1;
}
}
}

void __writev_nocancel_nostatus (int fd, const struct iovec *vec, size_t len)
{
assert (vec != 0);

for (unsigned i = 0; i < len; i++)
{
__write_nocancel (fd, vec[i].iov_base, vec[i].iov_len);
}
}

void __write_nocancel (int fd, const void *buf, size_t len)
{
assert (buf != 0);
assert (len > 0);

if (fd == STDERR_FILENO)
{
char msg[100];
if (len > sizeof msg-1)
{
len = sizeof msg-1;
}

memcpy (msg, buf, len);
msg[len] = '\0';

if ( len > 1
&& msg[len-1] == '\n')
{
msg[len-1] = '\0';
}

CLogger::Get ()->Write (From, LogError, msg);

return;
}

if (s_pFileSystem != 0)
{
assert (len < (unsigned) -1);

if (s_pFileSystem->FileWrite ((unsigned) fd, buf, (unsigned) len) != (unsigned) len)
{
CLogger::Get ()->Write (From, LogError, "Write failed");
}
}
else
{
assert (s_pFATFS != 0);

unsigned byteswritten;
if ( fd != FATFS_FILE_DESCRIPTOR
|| s_pFile == 0
|| f_write (s_pFile, (const char *) buf, len, &byteswritten) != FR_OK
|| byteswritten != len)
{
CLogger::Get ()->Write (From, LogError, "Write failed");
}
}
}

void __close_nocancel_nostatus (int fd)
{
if (s_pFileSystem != 0)
{
s_pFileSystem->FileClose ((unsigned) fd);
}
else
{
assert (s_pFATFS != 0);

if ( fd == FATFS_FILE_DESCRIPTOR
&& s_pFile != 0)
{
f_close (s_pFile);

delete s_pFile;
s_pFile = 0;
}
}
}
91 changes: 91 additions & 0 deletions addon/profile/glibc_compat.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
//
// glibc_compat.h
//
// Circle - A C++ bare metal environment for Raspberry Pi
// Copyright (C) 2020 R. Stange <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
#ifndef _profile_glibc_compat_h
#define _profile_glibc_compat_h

#include <circle/fs/fat/fatfs.h>
#include <fatfs/ff.h>
#include <circle/types.h>

#define __BEGIN_DECLS
#define __END_DECLS
#define __THROW

#define attribute_hidden
#define libc_hidden_def(symbol)
#define libc_hidden_proto(symbol)
#define weak_alias(symbol, alias)

#define NULL 0

typedef unsigned char u_char;
typedef unsigned short u_short;
typedef unsigned int u_int;
typedef unsigned long u_long;

struct iovec
{
void *iov_base;
size_t iov_len;
};

#define STDERR_FILENO 0

#define O_CREAT (1 << 0)
#define O_TRUNC (1 << 1)
#define O_WRONLY (1 << 2)
#define O_NOFOLLOW (1 << 3)

void __set_nocancel_filesystem (CFATFileSystem *fs);
void __set_nocancel_filesystem (FATFS *fs, const char *drive);

int __open_nocancel (const char *name, unsigned mode, unsigned umask);
void __writev_nocancel_nostatus (int fd, const struct iovec *vec, size_t len);
void __write_nocancel (int fd, const void *buf, size_t len);
void __close_nocancel_nostatus (int fd);

static inline bool atomic_compare_and_exchange_bool_acq (long *mem, long newval, long oldval)
{
long expected = oldval;

return !__atomic_compare_exchange_n (mem, &expected, newval, false,
#ifdef ARM_ALLOW_MULTI_CORE
__ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST
#else
__ATOMIC_RELAXED, __ATOMIC_RELAXED
#endif
);
}

static inline int ffs (int i)
{
int mask = 1;
for (int n = 1; n <= 32; n++, mask <<= 1)
{
if (i & mask)
{
return n;
}
}

return 0;
}

#endif
Loading

0 comments on commit bcf5216

Please sign in to comment.