2010-04-25 00:31:27 +00:00
|
|
|
/*
|
2009-02-09 21:15:56 +00:00
|
|
|
* Copyright (C) 2007-2009 Gabest
|
|
|
|
* http://www.gabest.org
|
|
|
|
*
|
|
|
|
* 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 2, or (at your option)
|
|
|
|
* any later version.
|
2010-04-25 00:31:27 +00:00
|
|
|
*
|
2009-02-09 21:15:56 +00:00
|
|
|
* 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.
|
2010-04-25 00:31:27 +00:00
|
|
|
*
|
2009-02-09 21:15:56 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with GNU Make; see the file COPYING. If not, write to
|
2012-09-09 18:16:11 +00:00
|
|
|
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA USA.
|
2009-02-09 21:15:56 +00:00
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "GS.h"
|
2009-05-11 08:18:00 +00:00
|
|
|
#include "GSCodeBuffer.h"
|
2011-02-16 03:19:36 +00:00
|
|
|
#include "xbyak/xbyak.h"
|
|
|
|
#include "xbyak/xbyak_util.h"
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-02-11 07:05:44 +00:00
|
|
|
template<class KEY, class VALUE> class GSFunctionMap
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
|
|
|
protected:
|
|
|
|
struct ActivePtr
|
|
|
|
{
|
2009-05-15 11:11:26 +00:00
|
|
|
uint64 frame, frames;
|
2013-06-23 10:46:24 +00:00
|
|
|
uint64 ticks, actual, total;
|
2009-02-11 07:05:44 +00:00
|
|
|
VALUE f;
|
2009-02-09 21:15:56 +00:00
|
|
|
};
|
|
|
|
|
2009-05-11 08:18:00 +00:00
|
|
|
hash_map<KEY, VALUE> m_map;
|
|
|
|
hash_map<KEY, ActivePtr*> m_map_active;
|
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
ActivePtr* m_active;
|
|
|
|
|
2009-02-11 07:05:44 +00:00
|
|
|
virtual VALUE GetDefaultFunction(KEY key) = 0;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
public:
|
2010-04-25 00:31:27 +00:00
|
|
|
GSFunctionMap()
|
|
|
|
: m_active(NULL)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~GSFunctionMap()
|
|
|
|
{
|
2009-07-04 17:37:06 +00:00
|
|
|
for_each(m_map_active.begin(), m_map_active.end(), delete_second());
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
2009-06-12 19:09:17 +00:00
|
|
|
VALUE operator [] (KEY key)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
|
|
|
m_active = NULL;
|
|
|
|
|
2011-02-19 03:36:30 +00:00
|
|
|
typename hash_map<KEY, ActivePtr*>::iterator i = m_map_active.find(key);
|
2009-05-11 08:18:00 +00:00
|
|
|
|
|
|
|
if(i != m_map_active.end())
|
|
|
|
{
|
2009-06-27 03:32:33 +00:00
|
|
|
m_active = i->second;
|
2009-05-11 08:18:00 +00:00
|
|
|
}
|
|
|
|
else
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2011-02-19 03:36:30 +00:00
|
|
|
typename hash_map<KEY, VALUE>::iterator i = m_map.find(key);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
ActivePtr* p = new ActivePtr();
|
|
|
|
|
|
|
|
memset(p, 0, sizeof(*p));
|
|
|
|
|
2009-05-15 11:11:26 +00:00
|
|
|
p->frame = (uint64)-1;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-06-27 03:32:33 +00:00
|
|
|
p->f = i != m_map.end() ? i->second : GetDefaultFunction(key);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-05-11 08:18:00 +00:00
|
|
|
m_map_active[key] = p;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
m_active = p;
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_active->f;
|
|
|
|
}
|
|
|
|
|
2013-06-23 10:46:24 +00:00
|
|
|
void UpdateStats(uint64 frame, uint64 ticks, int actual, int total)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
|
|
|
if(m_active)
|
|
|
|
{
|
|
|
|
if(m_active->frame != frame)
|
|
|
|
{
|
|
|
|
m_active->frame = frame;
|
|
|
|
m_active->frames++;
|
|
|
|
}
|
|
|
|
|
2011-12-18 08:13:20 +00:00
|
|
|
m_active->ticks += ticks;
|
2013-06-23 10:46:24 +00:00
|
|
|
m_active->actual += actual;
|
|
|
|
m_active->total += total;
|
|
|
|
|
|
|
|
ASSERT(m_active->total >= m_active->actual);
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void PrintStats()
|
|
|
|
{
|
2011-12-18 08:13:20 +00:00
|
|
|
uint64 ttpf = 0;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2011-02-19 03:36:30 +00:00
|
|
|
typename hash_map<KEY, ActivePtr*>::iterator i;
|
|
|
|
|
|
|
|
for(i = m_map_active.begin(); i != m_map_active.end(); i++)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-06-27 03:32:33 +00:00
|
|
|
ActivePtr* p = i->second;
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
if(p->frames)
|
|
|
|
{
|
|
|
|
ttpf += p->ticks / p->frames;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-16 03:19:36 +00:00
|
|
|
printf("GS stats\n");
|
|
|
|
|
2011-02-19 03:36:30 +00:00
|
|
|
for(i = m_map_active.begin(); i != m_map_active.end(); i++)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-06-27 03:32:33 +00:00
|
|
|
KEY key = i->first;
|
|
|
|
ActivePtr* p = i->second;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2015-10-14 13:44:22 +00:00
|
|
|
if(p->frames && ttpf)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2013-06-23 10:46:24 +00:00
|
|
|
uint64 tpp = p->actual > 0 ? p->ticks / p->actual : 0;
|
2011-12-18 08:13:20 +00:00
|
|
|
uint64 tpf = p->frames > 0 ? p->ticks / p->frames : 0;
|
2013-06-23 10:46:24 +00:00
|
|
|
uint64 ppf = p->frames > 0 ? p->actual / p->frames : 0;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2013-06-23 10:46:24 +00:00
|
|
|
printf("[%014llx]%c %6.2f%% %5.2f%% f %4lld t %12lld p %12lld w %12lld tpp %4lld tpf %9lld ppf %9lld\n",
|
2009-05-15 11:11:26 +00:00
|
|
|
(uint64)key, m_map.find(key) == m_map.end() ? '*' : ' ',
|
2013-06-23 10:46:24 +00:00
|
|
|
(float)(tpf * 10000 / 34000000) / 100,
|
2010-04-25 00:31:27 +00:00
|
|
|
(float)(tpf * 10000 / ttpf) / 100,
|
2013-06-23 10:46:24 +00:00
|
|
|
p->frames, p->ticks, p->actual, p->total - p->actual,
|
2009-02-09 21:15:56 +00:00
|
|
|
tpp, tpf, ppf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2009-02-13 09:28:51 +00:00
|
|
|
|
2011-02-16 03:19:36 +00:00
|
|
|
class GSCodeGenerator : public Xbyak::CodeGenerator
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
Xbyak::util::Cpu m_cpu;
|
|
|
|
|
|
|
|
public:
|
|
|
|
GSCodeGenerator(void* code, size_t maxsize)
|
|
|
|
: Xbyak::CodeGenerator(maxsize, code)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-02-13 09:28:51 +00:00
|
|
|
template<class CG, class KEY, class VALUE>
|
|
|
|
class GSCodeGeneratorFunctionMap : public GSFunctionMap<KEY, VALUE>
|
|
|
|
{
|
2009-05-11 08:18:00 +00:00
|
|
|
string m_name;
|
2011-02-16 03:19:36 +00:00
|
|
|
void* m_param;
|
|
|
|
hash_map<uint64, VALUE> m_cgmap;
|
2009-02-19 13:13:20 +00:00
|
|
|
GSCodeBuffer m_cb;
|
|
|
|
|
2013-06-24 01:11:00 +00:00
|
|
|
enum {MAX_SIZE = 8192};
|
2009-02-13 09:28:51 +00:00
|
|
|
|
|
|
|
public:
|
2011-02-16 03:19:36 +00:00
|
|
|
GSCodeGeneratorFunctionMap(const char* name, void* param)
|
2011-02-12 21:45:16 +00:00
|
|
|
: m_name(name)
|
2011-02-16 03:19:36 +00:00
|
|
|
, m_param(param)
|
2009-02-13 09:28:51 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE GetDefaultFunction(KEY key)
|
|
|
|
{
|
2011-02-16 03:19:36 +00:00
|
|
|
VALUE ret = NULL;
|
2009-02-13 09:28:51 +00:00
|
|
|
|
2011-02-19 03:36:30 +00:00
|
|
|
typename hash_map<uint64, VALUE>::iterator i = m_cgmap.find(key);
|
2009-05-11 08:18:00 +00:00
|
|
|
|
|
|
|
if(i != m_cgmap.end())
|
|
|
|
{
|
2011-02-16 03:19:36 +00:00
|
|
|
ret = i->second;
|
2009-05-11 08:18:00 +00:00
|
|
|
}
|
|
|
|
else
|
2009-02-13 09:28:51 +00:00
|
|
|
{
|
2011-02-16 03:19:36 +00:00
|
|
|
CG* cg = new CG(m_param, key, m_cb.GetBuffer(MAX_SIZE), MAX_SIZE);
|
2009-02-13 09:28:51 +00:00
|
|
|
|
2011-02-12 21:45:16 +00:00
|
|
|
ASSERT(cg->getSize() < MAX_SIZE);
|
2011-02-19 03:36:30 +00:00
|
|
|
|
2009-02-19 13:13:20 +00:00
|
|
|
m_cb.ReleaseBuffer(cg->getSize());
|
|
|
|
|
2011-02-16 03:19:36 +00:00
|
|
|
ret = (VALUE)cg->getCode();
|
|
|
|
|
|
|
|
m_cgmap[key] = ret;
|
2009-02-22 16:28:39 +00:00
|
|
|
|
2011-12-16 19:13:58 +00:00
|
|
|
#ifdef ENABLE_VTUNE
|
2011-02-19 03:36:30 +00:00
|
|
|
|
2009-02-22 16:28:39 +00:00
|
|
|
// vtune method registration
|
2011-02-19 03:36:30 +00:00
|
|
|
|
2011-02-24 04:55:35 +00:00
|
|
|
// if(iJIT_IsProfilingActive()) // always > 0
|
2011-02-12 21:45:16 +00:00
|
|
|
{
|
2011-02-19 03:36:30 +00:00
|
|
|
string name = format("%s<%016llx>()", m_name.c_str(), (uint64)key);
|
2009-02-22 16:28:39 +00:00
|
|
|
|
2011-02-12 21:45:16 +00:00
|
|
|
iJIT_Method_Load ml;
|
2009-02-22 16:28:39 +00:00
|
|
|
|
2011-02-12 21:45:16 +00:00
|
|
|
memset(&ml, 0, sizeof(ml));
|
2009-02-22 16:28:39 +00:00
|
|
|
|
2011-02-12 21:45:16 +00:00
|
|
|
ml.method_id = iJIT_GetNewMethodID();
|
|
|
|
ml.method_name = (char*)name.c_str();
|
|
|
|
ml.method_load_address = (void*)cg->getCode();
|
|
|
|
ml.method_size = (unsigned int)cg->getSize();
|
2009-02-22 16:28:39 +00:00
|
|
|
|
2011-02-12 21:45:16 +00:00
|
|
|
iJIT_NotifyEvent(iJVM_EVENT_TYPE_METHOD_LOAD_FINISHED, &ml);
|
2011-03-08 01:48:15 +00:00
|
|
|
/*
|
2013-06-20 05:07:52 +00:00
|
|
|
name = format("c:/temp1/%s_%016llx.bin", m_name.c_str(), (uint64)key);
|
2011-03-08 01:48:15 +00:00
|
|
|
|
|
|
|
if(FILE* fp = fopen(name.c_str(), "wb"))
|
|
|
|
{
|
|
|
|
fputc(0x0F, fp); fputc(0x0B, fp);
|
|
|
|
fputc(0xBB, fp); fputc(0x6F, fp); fputc(0x00, fp); fputc(0x00, fp); fputc(0x00, fp);
|
|
|
|
fputc(0x64, fp); fputc(0x67, fp); fputc(0x90, fp);
|
|
|
|
|
|
|
|
fwrite(cg->getCode(), cg->getSize(), 1, fp);
|
2013-06-20 05:07:52 +00:00
|
|
|
|
2011-03-08 01:48:15 +00:00
|
|
|
fputc(0xBB, fp); fputc(0xDE, fp); fputc(0x00, fp); fputc(0x00, fp); fputc(0x00, fp);
|
|
|
|
fputc(0x64, fp); fputc(0x67, fp); fputc(0x90, fp);
|
|
|
|
fputc(0x0F, fp); fputc(0x0B, fp);
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
*/
|
2011-02-12 21:45:16 +00:00
|
|
|
}
|
2011-02-16 03:19:36 +00:00
|
|
|
|
2011-05-25 02:17:27 +00:00
|
|
|
#endif
|
2011-02-19 03:36:30 +00:00
|
|
|
|
2011-02-16 03:19:36 +00:00
|
|
|
delete cg;
|
2009-02-13 09:28:51 +00:00
|
|
|
}
|
|
|
|
|
2011-02-16 03:19:36 +00:00
|
|
|
return ret;
|
2009-02-13 09:28:51 +00:00
|
|
|
}
|
|
|
|
};
|