1
0

new structure + serie 6

This commit is contained in:
Jacques Supcik
2022-05-11 15:14:01 +02:00
parent efeaeca819
commit 7c7c7f3e66
50 changed files with 325 additions and 0 deletions

View File

@@ -0,0 +1,5 @@
DIRS=$(filter-out Makefile, $(wildcard *))
all clean install clean_all:
for dir in $(DIRS); do $(MAKE) $@ -C $$dir; done

View File

@@ -0,0 +1,54 @@
EXE=ex1
SRCS=$(wildcard *.c)
ifeq ($(target),)
target=nano
endif
CFLAGS=-Wall -Wextra -g -c -O1 -MD -std=gnu11 -D_GNU_SOURCE
ifeq ($(target),nano)
TOOLCHAIN_PATH=/buildroot/output/host/usr/bin/
TOOLCHAIN=$(TOOLCHAIN_PATH)aarch64-linux-
CFLAGS+=-mcpu=cortex-a53 -funwind-tables
CFLAGS+=-fno-omit-frame-pointer
OBJDIR=.obj/nano
EXEC=$(EXE)
endif
ifeq ($(target),host)
EXEC=$(EXE)_h
endif
CC=$(TOOLCHAIN)gcc
LD=$(TOOLCHAIN)gcc
AR=$(TOOLCHAIN)ar
STRIP=$(TOOLCHAIN)strip
OBJDUMP=$(TOOLCHAIN)objdump
OBJDIR=.obj/$(target)
OBJS= $(addprefix $(OBJDIR)/, $(SRCS:.c=.o))
$(OBJDIR)/%o: %c
$(CC) $(CFLAGS) $< -o $@
all: $(OBJDIR)/ $(EXEC)
$(EXEC): $(OBJS) $(LINKER_SCRIPT)
$(LD) $(OBJS) $(LDFLAGS) -o $@
$(OBJDIR)/:
mkdir -p $(OBJDIR)
clean:
rm -Rf $(OBJDIR) $(EXEC) $(EXEC)_s *~ t.txt
clean_all: clean
rm -Rf .obj $(EXE) $(EXE)_s $(EXE)_a $(EXE)_a_s $(EXE)_h $(EXE)_h_s
dump: all
$(OBJDUMP) -dS $(EXEC) > t.txt
-include $(OBJS:.o=.d)
.PHONY: all clean clean_all dump

View File

@@ -0,0 +1,23 @@
#include <stdint.h>
#define SIZE 5000
static int32_t array[SIZE][SIZE];
int main (void)
{
int i, j, k;
for (k = 0; k < 10; k++)
{
for (i = 0; i < SIZE; i++)
{
for (j = 0; j < SIZE; j++)
{
array[j][i]++;
}
}
}
return 0;
}

View File

@@ -0,0 +1,54 @@
EXE=ex2
SRCS=$(wildcard *.c)
ifeq ($(target),)
target=nano
endif
CFLAGS=-Wall -Wextra -g -c -O0 -MD -std=gnu11 -D_GNU_SOURCE
ifeq ($(target),nano)
TOOLCHAIN_PATH=/buildroot/output/host/usr/bin/
TOOLCHAIN=$(TOOLCHAIN_PATH)aarch64-linux-
CFLAGS+=-mcpu=cortex-a53 -funwind-tables -fno-omit-frame-pointer
##CFLAGS+=-O2
OBJDIR=.obj/nano
EXEC=$(EXE)
endif
ifeq ($(target),host)
EXEC=$(EXE)_h
endif
CC=$(TOOLCHAIN)gcc
LD=$(TOOLCHAIN)gcc
AR=$(TOOLCHAIN)ar
STRIP=$(TOOLCHAIN)strip
OBJDUMP=$(TOOLCHAIN)objdump
OBJDIR=.obj/$(target)
OBJS= $(addprefix $(OBJDIR)/, $(SRCS:.c=.o))
$(OBJDIR)/%o: %c
$(CC) $(CFLAGS) $< -o $@
all: $(OBJDIR)/ $(EXEC)
$(EXEC): $(OBJS) $(LINKER_SCRIPT)
$(LD) $(OBJS) $(LDFLAGS) -o $@
$(OBJDIR)/:
mkdir -p $(OBJDIR)
clean:
rm -Rf $(OBJDIR) $(EXEC) $(EXEC)_s *~ t.txt
clean_all: clean
rm -Rf .obj $(EXE) $(EXE)_s $(EXE)_a $(EXE)_a_s $(EXE)_h $(EXE)_h_s
dump: all
$(OBJDUMP) -dS $(EXEC) > t.txt
-include $(OBJS:.o=.d)
.PHONY: all clean clean_all dump

View File

@@ -0,0 +1,24 @@
#include <stdio.h>
#include <stdlib.h>
#define SIZE 65536
int main()
{
// generate data
short data[SIZE];
for (int i = 0; i < SIZE; i++) {
data[i] = rand() % 512;
}
long long sum = 0;
for (int j = 0; j < 10000; j++) {
for (int i = 0; i < SIZE; i++) {
if (data[i] >= 256) {
sum += data[i];
}
}
}
printf ("sum=%lld\n", sum);
}

View File

@@ -0,0 +1,37 @@
#include "ApacheAccessLogAnalyzer.h"
#include <iostream>
#include <sstream>
#include <algorithm>
ApacheAccessLogAnalyzer::ApacheAccessLogAnalyzer(std::string filename)
: myFilename(filename)
{
}
void ApacheAccessLogAnalyzer::openFile()
{
myInFile.open(myFilename.c_str());
}
void ApacheAccessLogAnalyzer::closeFile()
{
myInFile.close();
}
void ApacheAccessLogAnalyzer::processFile()
{
std::cout << "Processing log file " << myFilename << std::endl;
for( std::string line; getline( myInFile, line ); )
{
// parse the log line to extract the hostname / ip address
int space_pos = line.find_first_of(" ");
std::string hostname = line.substr(0, space_pos);
myHostCounter.notifyHost(hostname);
}
std::cout << "Found " << myHostCounter.getNbOfHosts() << " unique Hosts/IPs" << std::endl;
}

View File

@@ -0,0 +1,20 @@
#include "HostCounter.h"
#include <string>
#include <vector>
#include <fstream>
class ApacheAccessLogAnalyzer
{
public:
ApacheAccessLogAnalyzer(std::string filename);
void openFile();
void closeFile();
void processFile();
private:
std::string myFilename;
std::ifstream myInFile;
HostCounter myHostCounter;
};

View File

@@ -0,0 +1,26 @@
#include "HostCounter.h"
#include <algorithm> // for std::find
HostCounter::HostCounter()
{
}
bool HostCounter::isNewHost(std::string hostname)
{
return std::find(myHosts.begin(), myHosts.end(), hostname) == myHosts.end();
}
void HostCounter::notifyHost(std::string hostname)
{
// add the host in the list if not already in
if(isNewHost(hostname))
{
myHosts.push_back(hostname);
}
}
int HostCounter::getNbOfHosts()
{
return myHosts.size();
}

View File

@@ -0,0 +1,22 @@
#include <string>
#include <vector>
class HostCounter
{
public:
HostCounter();
// Announce a host to the HostCounter.
// if the host is new, it will be added to the list, otherwise we ignore it.
void notifyHost(std::string hostname);
// return the number of unique hosts found so far
int getNbOfHosts();
private:
// check if host is already in the list
bool isNewHost(std::string hostname);
std::vector< std::string > myHosts;
};

View File

@@ -0,0 +1,30 @@
##CXX?=g++
##CXXFLAGS=-Wall -Wextra -g -O0 -MD
TOOLCHAIN_PATH=/buildroot/output/host/usr/bin/
TOOLCHAIN=$(TOOLCHAIN_PATH)aarch64-linux-
CXX=$(TOOLCHAIN)g++
CXXFLAGS=-Wall -Wextra -g -gdwarf -O0 -MD -mcpu=cortex-a53 -fno-omit-frame-pointer -funwind-tables
SOURCES=$(wildcard *.cpp)
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=read-apache-logs
all: $(SOURCES) $(EXECUTABLE)
$(EXECUTABLE): $(OBJECTS)
$(CXX) $(CXXFLAGS) -o $@ $(OBJECTS)
.c.o:
$(CXX) -c $(CXXFLAGS) $< -o $@
clean:
@rm -f $(OBJECTS)
@rm -f *.d *~
clean_all: clean
@rm -f $(EXECUTABLE)
@rm -f perf.data perf.data.old
-include *.d

View File

@@ -0,0 +1,30 @@
#include "ApacheAccessLogAnalyzer.h"
#include <iostream>
// forward declaration
void usage(const char* progName);
int main(int argc, const char* argv[])
{
if(argc != 2)
{
usage(argv[0]);
return -1;
}
std::string filename = argv[1];
ApacheAccessLogAnalyzer analyzer(filename);
analyzer.openFile();
analyzer.processFile();
analyzer.closeFile();
}
void usage(const char* progName)
{
std::cout << "Usage: " << progName << " <filename>" << std::endl;
std::cout << "\nWhere <filename> is the apache access log file" << std::endl;
}